Skip to content

Instantly share code, notes, and snippets.

@alexander-yakushev
Last active December 1, 2023 13:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexander-yakushev/06485fe945e30691a1f4d0c87be1f4d9 to your computer and use it in GitHub Desktop.
Save alexander-yakushev/06485fe945e30691a1f4d0c87be1f4d9 to your computer and use it in GitHub Desktop.
Advent of Code 2023, day 1
;; Didn't know yet that there is a special syntax to make regexes overlapping. Got angry and wrote this.
(require '[clojure.string :as str])
(def digits (->> ["one" "two" "three" "four" "five" "six" "seven" "eight" "nine"]
(map-indexed (fn [i w] [w (str (inc i))]))
(into {})))
(def rx (re-pattern (str/join "|" (conj (keys digits) "[0-9]"))))
(def rx-rev (re-pattern (str/join "|" (conj (map str/reverse (keys digits)) "[0-9]"))))
(defn extract-calibration [l]
(let [left (#(or (digits %) %) (re-find rx l))
right (#(or (digits %) %) (str/reverse (re-find rx-rev (str/reverse l))))]
(parse-long (str left right))))
(->> (slurp "day1.txt")
str/split-lines
(mapv extract-calibration)
(reduce +))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment