Skip to content

Commit

Permalink
day21.clj: 1.4x faster
Browse files Browse the repository at this point in the history
  • Loading branch information
narimiran committed Sep 12, 2024
1 parent 7bc26e9 commit 9ab7b05
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions clojure/day21.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
(def sq-size 131)
(def p2-squares (quot p2-steps sq-size))

(defn normalize [[x y]]
(+ (* 1000 (mod y sq-size))
(mod x sq-size)))


(defn calc-total [[a b c]]
(+ a
Expand All @@ -27,19 +31,25 @@
(if even-step?
(swap! p2 conj (count @seen-even))
(swap! p2 conj (count @seen-odd))))
(reduce (fn [c pt]
(into c (for [[x y] (aoc/neighbours 4 pt)
:let [nb [(mod x sq-size) (mod y sq-size)]]
:when (and (not (walls nb))
(if even-step?
(not (@seen-odd [x y]))
(not (@seen-even [x y]))))]
(do (if even-step?
(swap! seen-odd conj [x y])
(swap! seen-even conj [x y]))
[x y]))))
#{}
current)))
(persistent!
(reduce
(fn [c nbs]
(reduce (fn [c nb]
(if even-step?
(swap! seen-odd conj nb)
(swap! seen-even conj nb))
(conj! c nb))
c
nbs))
(transient #{})
(map (fn [pt] (aoc/neighbours
4 pt
(fn [nb]
(and (not (walls (normalize nb)))
(if even-step?
(not (@seen-odd nb))
(not (@seen-even nb)))))))
current)))))
[start]
(range (+ 1 65 (* 2 sq-size))))
[@p1 (calc-total @p2)]))
Expand All @@ -49,7 +59,7 @@
(let [input (aoc/parse-input input :chars)
size (count input)
start [(quot size 2) (quot size 2)]
walls (set (keys (aoc/grid->points input #{\#})))]
walls (aoc/grid->hashed-point-set input #{\#})]
(traverse walls start)))


Expand Down

0 comments on commit 9ab7b05

Please sign in to comment.