day 1, part 1

This commit is contained in:
Aleh Suprunovich 2023-12-01 17:42:28 +03:00
parent f62f28f61d
commit 509e797ba9
5 changed files with 1062 additions and 0 deletions

2
deps.edn Normal file
View File

@ -0,0 +1,2 @@
{:paths ["src" "inputs"]
:deps {org.clojure/clojure {:mvn/version "1.11.1"}}}

1000
inputs/day01 Normal file

File diff suppressed because it is too large Load Diff

4
inputs/day01.test.1 Normal file
View File

@ -0,0 +1,4 @@
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet

7
inputs/day01.test.2 Normal file
View File

@ -0,0 +1,7 @@
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen

49
src/day01.clj Normal file
View File

@ -0,0 +1,49 @@
(ns day01
(:require [clojure.string :as string]))
(def input (-> "inputs/day01.test.1"
slurp
(string/split #"\n")))
;; part one
(defn is-digit? [c]
(<= (int \0) (int c) (int \9)))
(defn calibration-value [s]
(let [digits (filter is-digit? s)]
(read-string (str (first digits)
(last digits)))))
(def sum-of-all-calibration-values
(reduce + (map calibration-value input)))
;; part two
(def input2 (-> "inputs/day01.test.2"
slurp
(string/split #"\n")))
(def replace-digits
{"one" "1"
"two" "2"
"three" "3"
"four" "4"
"five" "5"
"six" "6"
"seven" "7"
"eight" "8"
"nine" "9"})
(defn fix-line [l]
(let [replacements (->> (keys replace-digits)
(mapcat (fn [d] {d (string/index-of l d)}))
(filter (fn [[_ v]] (not (nil? v))))
(sort-by second))
fix-fn (apply comp
(map (fn [[r _]] (fn [s] (string/replace s r (replace-digits r))))
replacements))]
(fix-fn l)))
(def sum-of-actual-calibration-values
(reduce + (map (comp calibration-value fix-line) input2)))