aboutsummaryrefslogtreecommitdiffstats
path: root/day4
diff options
context:
space:
mode:
authorClyne Sullivan <clyne@bitgloo.com>2021-12-04 21:32:44 -0500
committerClyne Sullivan <clyne@bitgloo.com>2021-12-04 21:32:44 -0500
commit136b7866f04dce5642cd4344fdbbf7dfede4a994 (patch)
treed34c307f0e9a6600c6e4d7d0abc593c5344b4737 /day4
parentd9394b84b11b84ed5a5439a617e9aa4e4126637f (diff)
day4: made more concise
Diffstat (limited to 'day4')
-rw-r--r--day4/part1.clj94
-rw-r--r--day4/part2.clj92
2 files changed, 80 insertions, 106 deletions
diff --git a/day4/part1.clj b/day4/part1.clj
index eddebe6..b9fcceb 100644
--- a/day4/part1.clj
+++ b/day4/part1.clj
@@ -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)
-
diff --git a/day4/part2.clj b/day4/part2.clj
index e90137d..516e83c 100644
--- a/day4/part2.clj
+++ b/day4/part2.clj
@@ -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)
-