Skip to content

Commit

Permalink
2024: add day 03
Browse files Browse the repository at this point in the history
  • Loading branch information
RafDevX committed Dec 3, 2024
1 parent f7f8c24 commit 73bb2a4
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
1 change: 1 addition & 0 deletions 2024/src/day03/example1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))
1 change: 1 addition & 0 deletions 2024/src/day03/example2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))
38 changes: 38 additions & 0 deletions 2024/src/day03/part1.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
(ns day03.part1
(:require [clojure.test :refer [deftest]]
[util :refer [get-resource-path]]
[ysera.test :refer [is=]]))


(defn process-input
{:test (fn []
(is= (process-input ::example1)
[[2 4]
[5 5]
[11 8]
[8 5]]))}
[name]
(let [path (get-resource-path name)
text (slurp path)]
(->> (re-seq #"mul\((\d+),(\d+)\)" text)
(map rest)
(map (partial map ^[String] Integer/parseInt))
(map vec)
(vec))))


(defn solve
[name]
(->> (process-input name)
(map (partial reduce *))
(reduce +)))


(deftest example
(is= (solve ::example1)
161))


(deftest input
(is= (solve ::input)
188741603))
50 changes: 50 additions & 0 deletions 2024/src/day03/part2.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
(ns day03.part2
(:require [clojure.test :refer [deftest]]
[util :refer [get-resource-path]]
[ysera.test :refer [is=]]))


(defn process-input
{:test (fn []
(is= (process-input ::example2)
[[2 4]
false
[5 5]
[11 8]
true
[8 5]]))}
[name]
(let [path (get-resource-path name)
text (slurp path)]
(->> (re-seq #"mul\((\d+),(\d+)\)|do\(\)|don't\(\)" text)
(map (fn [m] (case (first m)
"do()" true
"don't()" false
(->> (rest m)
(map ^[String] Integer/parseInt)
(vec)))))
(vec))))


(defn solve
[name]
(->> (process-input name)
(reduce (fn [[acc active] instruction]
(cond
(boolean? instruction) [acc instruction]
active [(+ acc
(reduce * instruction))
active]
:else [acc active]))
[0 true])
(first)))


(deftest example
(is= (solve ::example2)
48))


(deftest input
(is= (solve ::input)
67269798))

0 comments on commit 73bb2a4

Please sign in to comment.