-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday12.clj
44 lines (34 loc) · 1.21 KB
/
day12.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
38
39
40
41
42
43
44
(ns day12
(:require aoc
[clojure.string :as str]
[clojure.core.match :refer [match]]))
(defn read-param [param]
(if-let [p (and (some? param)
(parse-long param))]
p
(keyword param)))
(defn parse-line [line]
(let [[a b c] (str/split line #" ")]
[(keyword a) (read-param b) (read-param c)]))
(defn play-instructions [instructions c]
(loop [i 0
regs {:a 0 :b 0 :c c :d 0}]
(match (get instructions i)
nil (regs :a)
[:cpy what where] (recur (inc i) (assoc regs where (or (regs what) what)))
[:jnz val jump]
(cond ; small optimization for an easy ~10.000x speedup
(= i 12) (recur (inc i)
(assoc regs
:a (+ (regs :a) (regs :b))
:b 0))
:else (if (zero? (or (regs val) val))
(recur (inc i) regs)
(recur (+ i jump) regs)))
[:inc reg nil] (recur (inc i) (update regs reg inc))
[:dec reg nil] (recur (inc i) (update regs reg dec)))))
(defn solve [input]
(let [instructions (vec (aoc/read-input input parse-line))]
(for [c (range 2)]
(play-instructions instructions c))))
(solve 12)