You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.3 KiB
Clojure

3 years ago
(require '[clojure.string :as str])
(def calls
(->> (read-line)
(#(str/split % #","))
(map #(Integer/parseInt %))
)
3 years ago
)
(defn read-board []
(when (some? (read-line))
(mapv
(fn [line] (->> line
(str/join)
(str/trim)
(#(str/split % #"\s+"))
(map #(Integer/parseInt %))
)
3 years ago
)
(repeatedly 5 read-line)
)
3 years ago
)
)
(defn bingo? [board]
(some?
(some
#(every? nil? %)
(concat board (apply mapv vector board))
3 years ago
)
)
)
(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))]
3 years ago
(if (bingo? new-board)
[new-board (first nums) turns]
(recur new-board (rest nums) (inc turns))
)
)
)
)
(loop [best [999 0] board (read-board)]
(if (nil? board)
(println best)
3 years ago
(let [bingo (get-bingo board)]
(recur
(if (< (get bingo 2) (first best))
[(get bingo 2)
(->> (first bingo)
(flatten)
(filter some?)
(apply +)
(* (second bingo))
3 years ago
)
]
best
)
(read-board)
3 years ago
)
)
)
)