mirror of
https://github.com/Ajetski/advent-of-code.git
synced 2025-09-30 13:03:19 -09:00
do day 4
This commit is contained in:
parent
57576bd5a4
commit
5366c4c09e
49
2023/src/day04.clj
Normal file
49
2023/src/day04.clj
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
(ns ^{:doc "Day 4"
|
||||||
|
:author "Adam Jeniski"}
|
||||||
|
day04 (:require
|
||||||
|
[clojure.math :as math]
|
||||||
|
[clojure.set :refer [intersection]]
|
||||||
|
[clojure.string :as string]
|
||||||
|
[core :refer [get-puzzle-input]]))
|
||||||
|
|
||||||
|
(def lines (get-puzzle-input 4))
|
||||||
|
|
||||||
|
(defn parse-nums [nums]
|
||||||
|
(->> (string/split nums #" ")
|
||||||
|
(filter not-empty)
|
||||||
|
(map #(Integer/parseInt %))
|
||||||
|
(into #{})))
|
||||||
|
|
||||||
|
;; part 1
|
||||||
|
(->> lines
|
||||||
|
(map #(re-find #"^Card\s*\d+: ([\s\d]+) \| ([\s\d]+)" %))
|
||||||
|
(map rest)
|
||||||
|
(map (partial map parse-nums))
|
||||||
|
(map (partial apply intersection))
|
||||||
|
(map count)
|
||||||
|
(map #(math/pow 2 (dec %)))
|
||||||
|
(map int)
|
||||||
|
(reduce +))
|
||||||
|
|
||||||
|
;; part 2
|
||||||
|
(loop [acc 0
|
||||||
|
data (->> lines
|
||||||
|
(map #(re-find #"^Card\s*\d+: ([\s\d]+) \| ([\s\d]+)" %))
|
||||||
|
(map rest)
|
||||||
|
(map (fn [[win actual]]
|
||||||
|
[1 (count (intersection (parse-nums win)
|
||||||
|
(parse-nums actual)))]))
|
||||||
|
(into []))]
|
||||||
|
(if (not-empty data)
|
||||||
|
(let [cnt-to-add (second (first data))
|
||||||
|
curr-cnt (ffirst data)]
|
||||||
|
|
||||||
|
(recur
|
||||||
|
(+ acc curr-cnt)
|
||||||
|
(concat (->> data
|
||||||
|
rest
|
||||||
|
(take cnt-to-add)
|
||||||
|
(map #(update % 0 + curr-cnt)))
|
||||||
|
(drop (inc cnt-to-add) data))))
|
||||||
|
acc))
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user