-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_23.clj
58 lines (51 loc) · 1.75 KB
/
day_23.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
(ns advent-of-code-2015.day-23
(:gen-class))
(defn new-machine
[instructions]
{:pc 0 :a 0 :b 0
:instrs (clojure.string/split-lines instructions)})
(defn run-instruction
[state instr]
(condp #(clojure.string/starts-with? %2 %1) instr
"hlf" (let [reg (keyword (subs instr 4))]
(-> state
(update reg #(/ % 2))
(update :pc inc)))
"tpl" (let [reg (keyword (subs instr 4))]
(-> state
(update reg (partial * 3))
(update :pc inc)))
"inc" (let [reg (keyword (subs instr 4))]
(-> state
(update reg inc)
(update :pc inc)))
"jmp" (let [offset (Integer. (subs instr 4))]
(update state :pc (partial + offset)))
"jie" (let [reg (keyword (subs instr 4 5))
offset (Integer. (subs instr 7))]
(update state :pc (if (even? (get state reg)) (partial + offset) inc)))
"jio" (let [reg (keyword (subs instr 4 5))
offset (Integer. (subs instr 7))]
(update state :pc (if (= 1 (get state reg)) (partial + offset) inc)))))
(defn run-machine
[instructions & {:keys [a b]
:or {a 0 b 0}}]
(loop [state (-> (new-machine instructions)
(assoc :a a :b b))]
(if (>= (:pc state)
(count (:instrs state)))
state
(recur (run-instruction state (get (:instrs state)
(:pc state)))))))
(defn part-1
[input]
(:b (run-machine input)))
(defn part-2
[input]
(:b (run-machine input :a 1)))
(defn day-23
[input-file]
(let [input (slurp input-file)]
(println "Day 23")
(println "Value of b register: " (part-1 input))
(println "Value of b register: " (part-2 input))))