/
day_07.clj
136 lines (113 loc) · 3.21 KB
/
day_07.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
(ns advent-of-code-2015.day-07
(:gen-class))
(defn parseShort
[s]
(try (Integer/parseUnsignedInt s)
(catch NumberFormatException e s)))
(defn gate-and
[x y]
(bit-and x y 0xffff))
(defn gate-or
[x y]
(bit-and (bit-or x y)
0xffff))
(defn gate-lshift
[x y]
(bit-and (bit-shift-left x y)
0xffff))
(defn gate-rshift
[x y]
(bit-and (unsigned-bit-shift-right x y)
0xffff))
(defn gate-not
[x]
(bit-and (bit-not x)
0xffff))
(defn connect-wire
[instruction]
(condp re-matches instruction
#"^([\d]+) -> ([a-z]+)$"
:>> #(list (nth % 2)
(parseShort (nth % 1)))
#"^([a-z]+) -> ([a-z]+)$"
:>> #(list (nth % 2)
(list identity (parseShort (nth % 1))))
#"^([a-z]+|[\d]+) AND ([a-z]+|[\d]+) -> ([a-z]+)$"
:>> #(list (nth % 3)
(list gate-and
(parseShort (nth % 1))
(parseShort (nth % 2))))
#"^([a-z]+|[\d]+) OR ([a-z]+|[\d]+) -> ([a-z]+)$"
:>> #(list (nth % 3)
(list gate-or
(parseShort (nth % 1))
(parseShort (nth % 2))))
#"^([a-z]+|[\d]+) LSHIFT ([\d]+) -> ([a-z]+)$"
:>> #(list (nth % 3)
(list gate-lshift
(parseShort (nth % 1))
(parseShort (nth % 2))))
#"^([a-z]+|[\d]+) RSHIFT ([\d]+) -> ([a-z]+)$"
:>> #(list (nth % 3)
(list gate-rshift
(parseShort (nth % 1))
(parseShort (nth % 2))))
#"^NOT ([a-z]+|[\d]+) -> ([a-z]+)"
:>> #(list (nth % 2)
(list gate-not
(parseShort (nth % 1))))))
(defn connect-circuit
[instructions]
(apply hash-map (mapcat identity (map connect-wire (clojure.string/split-lines instructions)))))
(defn update-wire
[circuit kv]
(let [[label value _] kv]
[label
(cond
(number? value)
value
(= (count value) 2)
(let [[op x _] value
x-val (get circuit x x)]
(if (number? x-val)
(op x-val)
value))
(= (count value) 3)
(let [[op x y _] value
x-val (get circuit x x)
y-val (get circuit y y)]
(if (and (number? x-val)
(number? y-val))
(op x-val y-val)
value)))]))
(defn simplify-circuit
([circuit]
(simplify-circuit circuit nil))
([circuit prev-circuit]
(if (or (every? (comp number? second) circuit)
(= circuit prev-circuit))
circuit
(recur (apply hash-map (mapcat identity (map (partial update-wire circuit) circuit)))
circuit))))
(def test-circuit
(connect-circuit "123 -> x
456 -> y
x AND y -> d
x OR y -> e
x LSHIFT 2 -> f
y RSHIFT 2 -> g
NOT x -> h
NOT y -> i"))
(defn part-1
[input]
(get (simplify-circuit (connect-circuit input)) "a"))
(defn part-2
[input]
(let [circuit (connect-circuit input)]
(get (simplify-circuit (assoc circuit "b" (part-1 input))) "a")))
(defn day-07
[input-file]
(let [input (slurp input-file)]
(println "Day 7")
(println "Value of wire \"a\": " (part-1 input))
(println "Value of wire \"a\" in new configuration: " (part-2 input))))