Day 4 and Day 5
This commit is contained in:
parent
2246504b1a
commit
70483bbfc1
128
2025.clj
128
2025.clj
@ -10,6 +10,8 @@
|
|||||||
(subvec coll (inc index)))
|
(subvec coll (inc index)))
|
||||||
(with-meta (meta coll))))
|
(with-meta (meta coll))))
|
||||||
|
|
||||||
|
(def directions {:N [-1 0] :S [1 0] :W [0 -1] :E [0 1]
|
||||||
|
:NW [-1 -1] :NE [-1 1] :SW [1 -1] :SE [1 1]})
|
||||||
;; _____ __
|
;; _____ __
|
||||||
;; | __ \ /_ |
|
;; | __ \ /_ |
|
||||||
;; | | | | __ _ _ _ | |
|
;; | | | | __ _ _ _ | |
|
||||||
@ -153,7 +155,6 @@ L82")
|
|||||||
(->> (str/split-lines input)
|
(->> (str/split-lines input)
|
||||||
(mapv #(mapv (fn [c] (Character/digit c 10)) (vec %)))
|
(mapv #(mapv (fn [c] (Character/digit c 10)) (vec %)))
|
||||||
(map (partial day3-get-max-battery total-digits))
|
(map (partial day3-get-max-battery total-digits))
|
||||||
taptap
|
|
||||||
(reduce +)))
|
(reduce +)))
|
||||||
|
|
||||||
(comment
|
(comment
|
||||||
@ -162,3 +163,128 @@ L82")
|
|||||||
(day3 12 day3-input-test)
|
(day3 12 day3-input-test)
|
||||||
(day3 12 (get-input 3))
|
(day3 12 (get-input 3))
|
||||||
:-)
|
:-)
|
||||||
|
|
||||||
|
;; _____ _ _
|
||||||
|
;; | __ \ | || |
|
||||||
|
;; | | | | __ _ _ _ | || |_
|
||||||
|
;; | | | |/ _` | | | | |__ _|
|
||||||
|
;; | |__| | (_| | |_| | | |
|
||||||
|
;; |_____/ \__,_|\__, | |_|
|
||||||
|
;; __/ |
|
||||||
|
;; |___/
|
||||||
|
|
||||||
|
(def day4-input-test
|
||||||
|
"..@@.@@@@.
|
||||||
|
@@@.@.@.@@
|
||||||
|
@@@@@.@.@@
|
||||||
|
@.@@@@..@.
|
||||||
|
@@.@@@@.@@
|
||||||
|
.@@@@@@@.@
|
||||||
|
.@.@.@.@@@
|
||||||
|
@.@@@.@@@@
|
||||||
|
.@@@@@@@@.
|
||||||
|
@.@.@@@.@.")
|
||||||
|
|
||||||
|
(defn day4-adjacent-count [grid [row col]]
|
||||||
|
(when (= \@ (get-in grid [row col]))
|
||||||
|
(->> (for [[dir [dy dx]] directions]
|
||||||
|
(get-in grid [(+ row dy) (+ col dx)]))
|
||||||
|
concat
|
||||||
|
(filter #(= % \@))
|
||||||
|
count)))
|
||||||
|
|
||||||
|
(defn day4-p1 [input]
|
||||||
|
(let [grid (str/split-lines input)]
|
||||||
|
(->> (for [row (range (count grid))
|
||||||
|
col (range (count (first grid)))]
|
||||||
|
(day4-adjacent-count grid [row col]))
|
||||||
|
(remove nil?)
|
||||||
|
(filter #(< % 4))
|
||||||
|
count)))
|
||||||
|
|
||||||
|
(defn day4-p2 [input]
|
||||||
|
(let [grid (str/split-lines input)
|
||||||
|
ROWS (count grid)
|
||||||
|
COLS (count (first grid))]
|
||||||
|
(loop [grid grid
|
||||||
|
removed-count 0]
|
||||||
|
(let [marked-cells (->> (for [row (range ROWS)
|
||||||
|
col (range COLS)]
|
||||||
|
(when-let [adj-count (day4-adjacent-count grid [row col])]
|
||||||
|
(when (< adj-count 4)
|
||||||
|
[row col])))
|
||||||
|
(remove nil?)
|
||||||
|
set)]
|
||||||
|
(if (empty? marked-cells)
|
||||||
|
removed-count
|
||||||
|
(recur (->> (for [row (range ROWS)
|
||||||
|
col (range COLS)]
|
||||||
|
(if (contains? marked-cells [row col])
|
||||||
|
\x
|
||||||
|
(get-in grid [row col])))
|
||||||
|
(partition COLS)
|
||||||
|
(mapv #(reduce str %)))
|
||||||
|
(+ removed-count (count marked-cells))))))))
|
||||||
|
|
||||||
|
(comment
|
||||||
|
(day4-p1 day4-input-test)
|
||||||
|
(day4-p1 (get-input 4))
|
||||||
|
(day4-p2 day4-input-test)
|
||||||
|
(day4-p2 (get-input 4))
|
||||||
|
:-)
|
||||||
|
|
||||||
|
;; _____ _____
|
||||||
|
;; | __ \ | ____|
|
||||||
|
;; | | | | __ _ _ _ | |__
|
||||||
|
;; | | | |/ _` | | | | |___ \
|
||||||
|
;; | |__| | (_| | |_| | ___) |
|
||||||
|
;; |_____/ \__,_|\__, | |____/
|
||||||
|
;; __/ |
|
||||||
|
;; |___/
|
||||||
|
|
||||||
|
(def day5-input-test
|
||||||
|
"3-5
|
||||||
|
10-14
|
||||||
|
16-20
|
||||||
|
12-18
|
||||||
|
|
||||||
|
1
|
||||||
|
5
|
||||||
|
8
|
||||||
|
11
|
||||||
|
17
|
||||||
|
32")
|
||||||
|
|
||||||
|
(defn day5-parse-input [input]
|
||||||
|
(let [[ranges ids] (->> (str/split input #"\n\n")
|
||||||
|
(map #(str/split-lines %)))]
|
||||||
|
[(map #(mapv parse-long (str/split % #"-")) ranges)
|
||||||
|
(map parse-long ids)]))
|
||||||
|
|
||||||
|
(defn day5-p1 [input]
|
||||||
|
(let [[ranges ids] (day5-parse-input input)]
|
||||||
|
(->> (map #(some (fn [[min max]] (<= min % max)) ranges) ids)
|
||||||
|
(remove nil?)
|
||||||
|
count)))
|
||||||
|
|
||||||
|
(defn day5-p2 [input]
|
||||||
|
(let [[ranges _] (day5-parse-input input)]
|
||||||
|
(->> (reduce (fn [acc elem]
|
||||||
|
(if (empty? acc)
|
||||||
|
(list elem)
|
||||||
|
(let [next-min (first elem)
|
||||||
|
[curr-min curr-max] (first acc)]
|
||||||
|
(if (<= curr-min next-min curr-max)
|
||||||
|
(cons [curr-min (max (second elem) curr-max)] (rest acc))
|
||||||
|
(cons elem acc)))))
|
||||||
|
'()
|
||||||
|
(sort ranges))
|
||||||
|
(map #(inc (- (second %) (first %))))
|
||||||
|
(reduce +))))
|
||||||
|
|
||||||
|
(comment
|
||||||
|
(day5-p1 day5-input-test)
|
||||||
|
(day5-p1 (get-input 5))
|
||||||
|
(day5-p2 day5-input-test)
|
||||||
|
(day5-p2 (get-input 5))
|
||||||
|
:-)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user