Skip to content

Commit

Permalink
24/3 in clj
Browse files Browse the repository at this point in the history
  • Loading branch information
enigma committed Dec 10, 2024
1 parent c17c2ab commit e62a174
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions clojure/y2024/d03.bb
Original file line number Diff line number Diff line change
@@ -0,0 +1,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)})

0 comments on commit e62a174

Please sign in to comment.