add day 4

master
Clyne 3 years ago
parent ebd7f129af
commit f413e05528

@ -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)

@ -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)
Loading…
Cancel
Save