-
Notifications
You must be signed in to change notification settings - Fork 2
/
8.clj
37 lines (32 loc) · 1.04 KB
/
8.clj
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
(ns advent-of-code.2017.8
(:require [clojure.string :as cs]
[clojure.java.io :as io]))
(def raw-input (slurp (io/resource "data_2017/8.txt")))
(defn line-> [l]
(let [[register op rhs _ regcomp comp val] (cs/split l #" ")
op-val (Integer/parseInt rhs)
update-op (case op "inc" + "dec" -)
update-fn #(update-op (or % 0) op-val)
compval (Integer/parseInt val)
comp-fn (case comp
"==" =
"!=" not=
(ns-resolve 'clojure.core (symbol comp)))]
{:predicate #(comp-fn (or (get % regcomp) 0) compval)
:instruction #(update % register update-fn)}))
(defn apply-instr [registers {:keys [predicate instruction]}]
(if (predicate registers)
(instruction registers)
registers))
;; solve part one
(->> (cs/split-lines raw-input)
(map line->)
(reduce apply-instr {})
(vals)
(apply max))
;; solve part two
(->> (cs/split-lines raw-input)
(map line->)
(reductions apply-instr {})
(mapcat vals)
(apply max))