Last active
December 1, 2023 13:53
-
-
Save alexander-yakushev/06485fe945e30691a1f4d0c87be1f4d9 to your computer and use it in GitHub Desktop.
Advent of Code 2023, day 1
This file contains 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
;; 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