day 3
This commit is contained in:
35
src/day3.clj
Normal file
35
src/day3.clj
Normal file
@@ -0,0 +1,35 @@
|
||||
(ns day3)
|
||||
|
||||
(def input (slurp "inputs/day3"))
|
||||
|
||||
(defn mul [instr]
|
||||
(->>
|
||||
instr
|
||||
(re-seq #"\d+")
|
||||
(map parse-long)
|
||||
(apply *)))
|
||||
|
||||
(->> input
|
||||
(re-seq #"mul\(\d+,\d+\)")
|
||||
(map mul)
|
||||
(reduce +)
|
||||
(println "result:"))
|
||||
|
||||
|
||||
(defn run2 [instructions]
|
||||
(loop
|
||||
[instructions
|
||||
(re-seq #"(don't\(\))|(do\(\))|mul\(\d+,\d+\)" instructions)
|
||||
do true
|
||||
result 0]
|
||||
(if (empty? instructions)
|
||||
result
|
||||
(let [instr (first (first instructions))]
|
||||
(case instr
|
||||
"do()" (recur (rest instructions) true result)
|
||||
"don't()" (recur (rest instructions) false result)
|
||||
(recur (rest instructions)
|
||||
do
|
||||
(if do (+ result (mul instr)) result)))))))
|
||||
|
||||
(println "result (part 2):" (run2 input))
|
||||
Reference in New Issue
Block a user