do 2017 day 8

This commit is contained in:
Adam Jeniski
2026-05-04 07:02:38 -04:00
parent 733c833f66
commit 9703c017a9
+46
View File
@@ -0,0 +1,46 @@
(ns day08
(:require
[clojure.string :as str]
[core]
[input-manager]))
(def op {"inc" +
"dec" -})
(def cond-op {"<" <
"<=" <=
">" >
">=" >=
"==" =
"!=" not=})
(def input (map (comp
(fn [[name operation num _ cond-name cond-operation cond-val]]
[name
(op operation)
(parse-long num)
cond-name
(cond-op cond-operation)
(parse-long cond-val)])
core/split-whitespace)
(input-manager/get-input 2017 8)))
(defn compute [acc [reg reg-op val cond-reg cond-op cond-val]]
(if (cond-op (acc cond-reg 0) cond-val)
(assoc acc reg (reg-op (acc reg 0) val))
acc))
;; part 1
(->> input
(reduce compute {})
(sort-by second)
last
second)
;; part 2
(->> input
(reductions compute {})
(mapcat identity)
set
(sort-by second)
last
second)