aboutsummaryrefslogtreecommitdiffstats
path: root/year2021/day4/part2.clj
diff options
context:
space:
mode:
Diffstat (limited to 'year2021/day4/part2.clj')
-rw-r--r--year2021/day4/part2.clj70
1 files changed, 70 insertions, 0 deletions
diff --git a/year2021/day4/part2.clj b/year2021/day4/part2.clj
new file mode 100644
index 0000000..516e83c
--- /dev/null
+++ b/year2021/day4/part2.clj
@@ -0,0 +1,70 @@
+(require '[clojure.string :as str])
+
+(def calls
+ (->> (read-line)
+ (#(str/split % #","))
+ (map #(Integer/parseInt %))
+ )
+ )
+
+(defn read-board []
+ (when (some? (read-line))
+ (mapv
+ (fn [line] (->> line
+ (str/join)
+ (str/trim)
+ (#(str/split % #"\s+"))
+ (map #(Integer/parseInt %))
+ )
+ )
+ (repeatedly 5 read-line)
+ )
+ )
+ )
+
+(defn bingo? [board]
+ (some?
+ (some
+ #(every? nil? %)
+ (concat board (apply mapv vector board))
+ )
+ )
+ )
+
+(defn bingo-mark [board n]
+ (mapv (partial replace {n nil}) board)
+ )
+
+(defn get-bingo [init-board]
+ (loop [board init-board nums calls turns 1]
+ (let [new-board (bingo-mark board (first nums))]
+ (if (bingo? new-board)
+ [new-board (first nums) turns]
+ (recur new-board (rest nums) (inc turns))
+ )
+ )
+ )
+ )
+
+(loop [best [0 0] board (read-board)]
+ (if (nil? board)
+ (println best)
+ (let [bingo (get-bingo board)]
+ (recur
+ (if (> (get bingo 2) (first best))
+ [(get bingo 2)
+ (->> (first bingo)
+ (flatten)
+ (filter some?)
+ (apply +)
+ (* (second bingo))
+ )
+ ]
+ best
+ )
+ (read-board)
+ )
+ )
+ )
+ )
+