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) (str/join)
(concat " ") (str/trim)
(str/join) (#(str/split % #"\s+"))
(str/trim) (map #(Integer/parseInt %))
(str/split #"\s+") )
(#(map (fn [l] (Integer/parseInt l)) %))
) )
)) (repeatedly 5 read-line)
)
) )
) )
(defn bingo-row? [row]
(every? nil? row)
)
(defn bingo? [board] (defn bingo? [board]
(not (some?
(nil? (some
(some #(every? nil? %)
bingo-row? (concat board (apply mapv vector board))
(concat
board
(apply mapv vector board)
)
)
) )
) )
) )
(defn get-bingo [_board] (defn bingo-mark [board n]
(loop (mapv (partial replace {n nil}) board)
[board _board nums calls turns 1] )
(let [new-board (vec (for [r (range 0 5)]
(replace {(first nums) nil} (get board r))))] (defn get-bingo [init-board]
(loop [board init-board nums calls turns 1]
(let [new-board (bingo-mark board (first nums))]
(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)
(* (->> (first bingo)
(second bingo) (flatten)
(->> (first bingo) (filter some?)
(flatten) (apply +)
(filter some?) (* (second bingo))
(apply +)
)
)]
) )
]
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) (str/join)
(concat " ") (str/trim)
(str/join) (#(str/split % #"\s+"))
(str/trim) (map #(Integer/parseInt %))
(str/split #"\s+") )
(#(map (fn [l] (Integer/parseInt l)) %))
) )
)) (repeatedly 5 read-line)
)
) )
) )
(defn bingo-row? [row]
(every? nil? row)
)
(defn bingo? [board] (defn bingo? [board]
(not (some?
(nil? (some
(some #(every? nil? %)
bingo-row? (concat board (apply mapv vector board))
(concat
board
(apply mapv vector board)
)
)
) )
) )
) )
(defn get-bingo [_board] (defn bingo-mark [board n]
(loop (mapv (partial replace {n nil}) board)
[board _board nums calls turns 1] )
(let [new-board (vec (for [r (range 0 5)]
(replace {(first nums) nil} (get board r))))] (defn get-bingo [init-board]
(loop [board init-board nums calls turns 1]
(let [new-board (bingo-mark board (first nums))]
(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)
(* (->> (first bingo)
(second bingo) (flatten)
(->> (first bingo) (filter some?)
(flatten) (apply +)
(filter some?) (* (second bingo))
(apply +)
)
)]
) )
]
best
)
(read-board)
) )
) )
(recur (read-board))
) )
) )
(println @best)

Loading…
Cancel
Save