day 1, part 1
This commit is contained in:
parent
f62f28f61d
commit
509e797ba9
2
deps.edn
Normal file
2
deps.edn
Normal file
@ -0,0 +1,2 @@
|
||||
{:paths ["src" "inputs"]
|
||||
:deps {org.clojure/clojure {:mvn/version "1.11.1"}}}
|
||||
1000
inputs/day01
Normal file
1000
inputs/day01
Normal file
File diff suppressed because it is too large
Load Diff
4
inputs/day01.test.1
Normal file
4
inputs/day01.test.1
Normal file
@ -0,0 +1,4 @@
|
||||
1abc2
|
||||
pqr3stu8vwx
|
||||
a1b2c3d4e5f
|
||||
treb7uchet
|
||||
7
inputs/day01.test.2
Normal file
7
inputs/day01.test.2
Normal file
@ -0,0 +1,7 @@
|
||||
two1nine
|
||||
eightwothree
|
||||
abcone2threexyz
|
||||
xtwone3four
|
||||
4nineeightseven2
|
||||
zoneight234
|
||||
7pqrstsixteen
|
||||
49
src/day01.clj
Normal file
49
src/day01.clj
Normal 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)))
|
||||
Loading…
x
Reference in New Issue
Block a user