Last active
December 3, 2023 10:58
-
-
Save buntine/10396e47eaaea2f31401237115a4f9c5 to your computer and use it in GitHub Desktop.
advent-of-code-2023-day3.clj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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