Skip to content

Commit

Permalink
day 1
Browse files Browse the repository at this point in the history
  • Loading branch information
narimiran committed Dec 1, 2023
1 parent 6a55dd2 commit e7a78bc
Show file tree
Hide file tree
Showing 5 changed files with 1,039 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ I currently don't have the time to solve in multiple languages, but I suspect I'
Task | Clojure | Comment
--- | --- | ---
Day 00: Helper file | [aoc.clj](clojure/aoc.clj) | Utilities I use to solve the tasks.
<!-- [Day 01](http://adventofcode.com/2023/day/1) | [day01.clj](clojure/day01.clj) | -->
[Day 01](http://adventofcode.com/2023/day/1) | [day01.clj](clojure/day01.clj) | Tougher than expected for day 1.
<!-- [Day 02](http://adventofcode.com/2023/day/2) | [day02.clj](clojure/day02.clj) | -->
<!-- [Day 03](http://adventofcode.com/2023/day/3) | [day03.clj](clojure/day03.clj) | -->
<!-- [Day 04](http://adventofcode.com/2023/day/4) | [day04.clj](clojure/day04.clj) | -->
Expand Down
41 changes: 35 additions & 6 deletions clojure/day01.clj
Original file line number Diff line number Diff line change
@@ -1,14 +1,43 @@
(ns day01
(:require aoc))
(:require aoc
[clojure.string :as str]))


(defn parse-line [line]
line)
(def words
{"one" "1"
"two" "2"
"three" "3"
"four" "4"
"five" "5"
"six" "6"
"seven" "7"
"eight" "8"
"nine" "9"})


(def word-pattern (apply str (interpose "|" (keys words))))
(def first-patt (re-pattern (str word-pattern #"|\d")))
(def last-patt (re-pattern (str (str/reverse word-pattern) #"|\d")))


(defn calibration-value [line patt rev-patt]
(let [first-match (re-find patt line)
last-match (str/reverse (re-find rev-patt (str/reverse line)))
first-digit (words first-match first-match)
last-digit (words last-match last-match)]
(parse-long (str first-digit last-digit))))


(defn calibration-sum [lines patt rev-patt]
(->> lines
(map #(calibration-value % patt rev-patt))
(reduce +)))


(defn solve [input-file]
(let [input (aoc/read-input input-file :int)]
input))
(let [lines (aoc/read-input input-file)]
[(calibration-sum lines #"\d" #"\d")
(calibration-sum lines first-patt last-patt)]))


(solve 1)
(solve 1)
5 changes: 3 additions & 2 deletions clojure/tests/solutions_tests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
test-input (str day "_test")
real-input day]
`(deftest ~test-name
(is (= (~solve-fn ~test-input) ~test-results))
(when ~test-results
(is (= (~solve-fn ~test-input) ~test-results)))
(is (= (~solve-fn ~real-input) ~real-results)))))




(check-day 1 [1 2] [3 4])
(check-day 1 nil [56049 54530])


(let [summary (run-tests)]
Expand Down
Loading

0 comments on commit e7a78bc

Please sign in to comment.