day4: made more concise

master
Clyne 3 years ago
parent d9394b84b1
commit 136b7866f0

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

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

Loading…
Cancel
Save