aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorClyne Sullivan <clyne@bitgloo.com>2021-12-04 19:53:16 -0500
committerClyne Sullivan <clyne@bitgloo.com>2021-12-04 19:53:16 -0500
commitf413e055286d1b7877b3512cf3170d928d8a1ee7 (patch)
tree46e26e10c6813890737ba36a094d72bbfdea2351
parentebd7f129afc2f1137667344096548661273baaad (diff)
add day 4
-rw-r--r--day4/part1.clj84
-rw-r--r--day4/part2.clj82
2 files changed, 166 insertions, 0 deletions
diff --git a/day4/part1.clj b/day4/part1.clj
new file mode 100644
index 0000000..eddebe6
--- /dev/null
+++ b/day4/part1.clj
@@ -0,0 +1,84 @@
+(require '[clojure.string :as str])
+
+(def calls
+ (map
+ #(Integer/parseInt %)
+ (-> (read-line)
+ (str/split #",")
+ )
+ )
+ )
+
+;(println calls)
+
+(defn read-board []
+ (when (not (nil? (read-line)))
+ (vec
+ (for [x (range 0 5)]
+ (-> (read-line)
+ (concat " ")
+ (str/join)
+ (str/trim)
+ (str/split #"\s+")
+ (#(map (fn [l] (Integer/parseInt l)) %))
+ )
+ ))
+ )
+ )
+
+(defn bingo-row? [row]
+ (every? nil? row)
+ )
+
+(defn bingo? [board]
+ (not
+ (nil?
+ (some
+ bingo-row?
+ (concat
+ board
+ (apply mapv vector board)
+ )
+ )
+ )
+ )
+ )
+
+(defn get-bingo [_board]
+ (loop
+ [board _board nums calls turns 1]
+ (let [new-board (vec (for [r (range 0 5)]
+ (replace {(first nums) nil} (get board r))))]
+ (if (bingo? new-board)
+ [new-board (first nums) turns]
+ (recur new-board (rest nums) (inc turns))
+ )
+ )
+ )
+ )
+
+(def best (atom [999 0]))
+
+(loop [board (read-board)]
+ (when (not (nil? board))
+ (let [bingo (get-bingo board)]
+ (when (< (get bingo 2) (first @best))
+ (reset! best
+ [(get bingo 2)
+ (*
+ (second bingo)
+ (->> (first bingo)
+ (flatten)
+ (filter some?)
+ (apply +)
+ )
+ )]
+ )
+ )
+ )
+ (recur (read-board))
+ )
+ )
+
+(println @best)
+
diff --git a/day4/part2.clj b/day4/part2.clj
new file mode 100644
index 0000000..e90137d
--- /dev/null
+++ b/day4/part2.clj
@@ -0,0 +1,82 @@
+(require '[clojure.string :as str])
+
+(def calls
+ (map
+ #(Integer/parseInt %)
+ (-> (read-line)
+ (str/split #",")
+ )
+ )
+ )
+
+(defn read-board []
+ (when (not (nil? (read-line)))
+ (vec
+ (for [x (range 0 5)]
+ (-> (read-line)
+ (concat " ")
+ (str/join)
+ (str/trim)
+ (str/split #"\s+")
+ (#(map (fn [l] (Integer/parseInt l)) %))
+ )
+ ))
+ )
+ )
+
+(defn bingo-row? [row]
+ (every? nil? row)
+ )
+
+(defn bingo? [board]
+ (not
+ (nil?
+ (some
+ bingo-row?
+ (concat
+ board
+ (apply mapv vector board)
+ )
+ )
+ )
+ )
+ )
+
+(defn get-bingo [_board]
+ (loop
+ [board _board nums calls turns 1]
+ (let [new-board (vec (for [r (range 0 5)]
+ (replace {(first nums) nil} (get board r))))]
+ (if (bingo? new-board)
+ [new-board (first nums) turns]
+ (recur new-board (rest nums) (inc turns))
+ )
+ )
+ )
+ )
+
+(def best (atom [0 0]))
+
+(loop [board (read-board)]
+ (when (not (nil? board))
+ (let [bingo (get-bingo board)]
+ (when (> (get bingo 2) (first @best))
+ (reset! best
+ [(get bingo 2)
+ (*
+ (second bingo)
+ (->> (first bingo)
+ (flatten)
+ (filter some?)
+ (apply +)
+ )
+ )]
+ )
+ )
+ )
+ (recur (read-board))
+ )
+ )
+
+(println @best)
+