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.
83 lines
1.5 KiB
Clojure
83 lines
1.5 KiB
Clojure
3 years ago
|
(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)
|
||
|
|