day4: made more concise

master
Clyne 3 years ago
parent d9394b84b1
commit 136b7866f0

@ -1,54 +1,43 @@
(require '[clojure.string :as str]) (require '[clojure.string :as str])
(def calls (def calls
(map (->> (read-line)
#(Integer/parseInt %) (#(str/split % #","))
(-> (read-line) (map #(Integer/parseInt %))
(str/split #",")
) )
) )
)
;(println calls)
(defn read-board [] (defn read-board []
(when (not (nil? (read-line))) (when (some? (read-line))
(vec (mapv
(for [x (range 0 5)] (fn [line] (->> line
(-> (read-line)
(concat " ")
(str/join) (str/join)
(str/trim) (str/trim)
(str/split #"\s+") (#(str/split % #"\s+"))
(#(map (fn [l] (Integer/parseInt l)) %)) (map #(Integer/parseInt %))
) )
)) )
(repeatedly 5 read-line)
) )
) )
(defn bingo-row? [row]
(every? nil? row)
) )
(defn bingo? [board] (defn bingo? [board]
(not (some?
(nil?
(some (some
bingo-row? #(every? nil? %)
(concat (concat board (apply mapv vector board))
board
(apply mapv vector board)
)
) )
) )
) )
(defn bingo-mark [board n]
(mapv (partial replace {n nil}) board)
) )
(defn get-bingo [_board] (defn get-bingo [init-board]
(loop (loop [board init-board nums calls turns 1]
[board _board nums calls turns 1] (let [new-board (bingo-mark board (first nums))]
(let [new-board (vec (for [r (range 0 5)]
(replace {(first nums) nil} (get board r))))]
(if (bingo? new-board) (if (bingo? new-board)
[new-board (first nums) turns] [new-board (first nums) turns]
(recur new-board (rest nums) (inc turns)) (recur new-board (rest nums) (inc turns))
@ -57,28 +46,25 @@
) )
) )
(def best (atom [999 0])) (loop [best [999 0] board (read-board)]
(if (nil? board)
(loop [board (read-board)] (println best)
(when (not (nil? board))
(let [bingo (get-bingo board)] (let [bingo (get-bingo board)]
(when (< (get bingo 2) (first @best)) (recur
(reset! best (if (< (get bingo 2) (first best))
[(get bingo 2) [(get bingo 2)
(*
(second bingo)
(->> (first bingo) (->> (first bingo)
(flatten) (flatten)
(filter some?) (filter some?)
(apply +) (apply +)
(* (second bingo))
) )
)] ]
best
) )
(read-board)
) )
) )
(recur (read-board))
) )
) )
(println @best)

@ -1,52 +1,43 @@
(require '[clojure.string :as str]) (require '[clojure.string :as str])
(def calls (def calls
(map (->> (read-line)
#(Integer/parseInt %) (#(str/split % #","))
(-> (read-line) (map #(Integer/parseInt %))
(str/split #",")
)
) )
) )
(defn read-board [] (defn read-board []
(when (not (nil? (read-line))) (when (some? (read-line))
(vec (mapv
(for [x (range 0 5)] (fn [line] (->> line
(-> (read-line)
(concat " ")
(str/join) (str/join)
(str/trim) (str/trim)
(str/split #"\s+") (#(str/split % #"\s+"))
(#(map (fn [l] (Integer/parseInt l)) %)) (map #(Integer/parseInt %))
) )
)) )
(repeatedly 5 read-line)
) )
) )
(defn bingo-row? [row]
(every? nil? row)
) )
(defn bingo? [board] (defn bingo? [board]
(not (some?
(nil?
(some (some
bingo-row? #(every? nil? %)
(concat (concat board (apply mapv vector board))
board
(apply mapv vector board)
)
) )
) )
) )
(defn bingo-mark [board n]
(mapv (partial replace {n nil}) board)
) )
(defn get-bingo [_board] (defn get-bingo [init-board]
(loop (loop [board init-board nums calls turns 1]
[board _board nums calls turns 1] (let [new-board (bingo-mark board (first nums))]
(let [new-board (vec (for [r (range 0 5)]
(replace {(first nums) nil} (get board r))))]
(if (bingo? new-board) (if (bingo? new-board)
[new-board (first nums) turns] [new-board (first nums) turns]
(recur new-board (rest nums) (inc turns)) (recur new-board (rest nums) (inc turns))
@ -55,28 +46,25 @@
) )
) )
(def best (atom [0 0])) (loop [best [0 0] board (read-board)]
(if (nil? board)
(loop [board (read-board)] (println best)
(when (not (nil? board))
(let [bingo (get-bingo board)] (let [bingo (get-bingo board)]
(when (> (get bingo 2) (first @best)) (recur
(reset! best (if (> (get bingo 2) (first best))
[(get bingo 2) [(get bingo 2)
(*
(second bingo)
(->> (first bingo) (->> (first bingo)
(flatten) (flatten)
(filter some?) (filter some?)
(apply +) (apply +)
(* (second bingo))
) )
)] ]
best
) )
(read-board)
) )
) )
(recur (read-board))
) )
) )
(println @best)

Loading…
Cancel
Save