-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd03.bb
43 lines (31 loc) · 1.01 KB
/
d03.bb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env bb
(ns y2024.d03
(:require [babashka.cli :as cli]))
(def cli-options {:input {:default "../../inputs/2024/03.input" :type :string}})
(def opts (cli/parse-opts *command-line-args* {:spec cli-options}))
(def input
(->> (slurp (get opts :input))))
(defn solve [data part2?]
(let [re #"mul\((\d+),(\d+)\)|do\(\)|don't\(\)"]
(loop [matches (re-seq re data)
result 0
doing? true]
(if (empty? matches)
result
(let [[match & rst] (first matches)
[res do?]
(cond
(= match "do()") [result true]
(= match "don't()") [result false]
(or doing? (not part2?))
(let [[a b] (map parse-long rst)]
[(+ result (* a b)) doing?])
:else
[result doing?])]
(recur (rest matches) res do?))))))
(defn part1 [data]
(solve data false))
(defn part2 [data]
(solve data true))
(prn {:part1 (part1 input)
:part2 (part2 input)})