Skip to content

Instantly share code, notes, and snippets.

@buntine
Last active December 3, 2023 10:58
Show Gist options
  • Save buntine/10396e47eaaea2f31401237115a4f9c5 to your computer and use it in GitHub Desktop.
Save buntine/10396e47eaaea2f31401237115a4f9c5 to your computer and use it in GitHub Desktop.
advent-of-code-2023-day3.clj
(ns advent-of-code-2023.day3)
(defn numbers [s]
(loop [m (re-matcher #"(?m)\d+" s)
res {}]
(if (.find m)
(recur m (assoc res [(.start m) (.end m)] (Integer. (.group m))))
res)))
(defn is-part?
[schematic size [[start end] n]]
(letfn [(has-sym? [[s e]]
(when (and (< 0 s (.length schematic))
(< 0 e (.length schematic)))
(boolean
(re-find #"[^\.\d]" (subs schematic s e)))))]
(->> [[(- start 1) start]
[end (+ end 1)]
[(- start size 1) (- end size -1)]
[(+ start size -1) (+ end size 1)]]
(some has-sym?))))
(defn part-sum
[size schematic]
(->> schematic
numbers
(filter (partial is-part? schematic size))
vals
(reduce +)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment