-
Notifications
You must be signed in to change notification settings - Fork 3
/
16.rb
executable file
·318 lines (272 loc) · 6.29 KB
/
16.rb
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/usr/bin/env ruby
class Time
def to_ms
(self.to_f * 1000.0).to_i
end
end
class Compute
def addr(regs, a, b, c)
r = regs.dup
r[c] = r[a] + r[b]
r
end
def addi(regs, a, b, c)
r = regs.dup
r[c] = r[a] + b
r
end
def mulr(regs, a, b, c)
r = regs.dup
r[c] = r[a] * r[b]
r
end
def muli(regs, a, b, c)
r = regs.dup
r[c] = r[a] * b
r
end
def banr(regs, a, b, c)
r = regs.dup
r[c] = r[a] & r[b]
r
end
def bani(regs, a, b, c)
r = regs.dup
r[c] = r[a] & b
r
end
def borr(regs, a, b, c)
r = regs.dup
r[c] = r[a] | r[b]
r
end
def bori(regs, a, b, c)
r = regs.dup
r[c] = r[a] | b
r
end
def setr(regs, a, _b, c)
r = regs.dup
r[c] = r[a]
r
end
def seti(regs, a, _b, c)
r = regs.dup
r[c] = a
r
end
def gtir(regs, a, b, c)
r = regs.dup
r[c] = a > r[b] ? 1 : 0
r
end
def gtri(regs, a, b, c)
r = regs.dup
r[c] = r[a] > b ? 1 : 0
r
end
def gtrr(regs, a, b, c)
r = regs.dup
r[c] = r[a] > r[b] ? 1 : 0
r
end
def eqir(regs, a, b, c)
r = regs.dup
r[c] = a == r[b] ? 1 : 0
r
end
def eqri(regs, a, b, c)
r = regs.dup
r[c] = r[a] == b ? 1 : 0
r
end
def eqrr(regs, a, b, c)
r = regs.dup
r[c] = r[a] == r[b] ? 1 : 0
r
end
def all_instructions
%w[
addr
addi
mulr
muli
banr
bani
borr
bori
setr
seti
gtir
gtri
gtrr
eqir
eqri
eqrr
]
end
end
require 'pp'
def tests
opcode_tests
p2_tests
unless which_opcodes_match([3, 2, 1, 1], [3, 2, 2, 1], 2, 1, 2) ==
%w[addi mulr seti]
raise 'fail0'
end
unless how_many_opcodes_match([3, 2, 1, 1], [3, 2, 2, 1], 2, 1, 2) == 3
raise 'fail1'
end
raise 'fail2' unless part1('input_1.txt') == 570
end
def p2_tests
opcodes = determine_opcodes('input_1.txt')
regs = part2('input_2.txt', opcodes)
raise 'fail p2' unless regs[0] == 503
end
def opcode_tests
# A = 3 Value A = 3 Register A = 0
# B = 2 Value B = 2 Register B = 3
# C = 3
# Something that sets register 3.
# Before: [0, 0, 3, 0]
# 14 3 2 3
# After: [0, 0, 3, 1]
cpu = Compute.new
regs_before = [0, 0, 3, 0]
regs_after = cpu.public_send('eqir', regs_before, 3, 2, 3)
regs_expect = [0, 0, 3, 1]
raise 'fail3' unless regs_after == regs_expect
end
def which_opcodes_match(regs_before, regs_after, a, b, c)
matches = []
cpu = Compute.new
cpu.all_instructions.each do |method_name|
if cpu.public_send(method_name, regs_before, a, b, c) == regs_after
matches.push method_name
end
end
matches.sort
end
def how_many_opcodes_match(regs_before, regs_after, a, b, c)
which_opcodes_match(regs_before, regs_after, a, b, c).count
end
def parse_mystery_input(filename)
mystery_inputs = []
File.open(filename, 'r') do |fh|
loop do
line = fh.gets
break if line.nil?
before_regs =
line.strip.match(/Before:\s+\[(\d+), (\d+), (\d+), (\d+)\]/).captures
.map(&:to_i)
line = fh.gets
break if line.nil?
opcode, a, b, c =
line.strip.match(/(\d+) (\d+) (\d+) (\d+)/).captures.map(&:to_i)
line = fh.gets
break if line.nil?
after_regs =
line.strip.match(/After:\s+\[(\d+), (\d+), (\d+), (\d+)\]/).captures
.map(&:to_i)
mystery_input = {
before_regs: before_regs,
after_regs: after_regs,
opcode: opcode,
a: a,
b: b,
c: c
}
mystery_inputs.push mystery_input
# Blank line
line = fh.gets
break if line.nil?
end
end
mystery_inputs
end
def part1(filename)
how_many_behave_like_three_or_more = 0
mystery_inputs = parse_mystery_input(filename)
mystery_inputs.each do |x|
matches =
how_many_opcodes_match(
x[:before_regs],
x[:after_regs],
x[:a],
x[:b],
x[:c]
)
how_many_behave_like_three_or_more += 1 if matches >= 3
end
how_many_behave_like_three_or_more
end
def part2(filename, opcodes)
cpu = Compute.new
instructions = parse_program_file(filename)
regs = [0, 0, 0, 0]
instructions.each do |i|
opcode = opcodes[i[:opcode]]
raise 'Invalid opcode' if opcode.nil?
regs = cpu.public_send(opcode, regs, i[:a], i[:b], i[:c])
end
regs
end
def parse_program_file(filename)
instructions = []
File.readlines(filename).each do |line|
opcode, a, b, c =
line.strip.match(/(\d+) (\d+) (\d+) (\d+)/).captures.map(&:to_i)
instruction = { opcode: opcode, a: a, b: b, c: c }
instructions.push instruction
end
instructions
end
def determine_opcodes(filename)
opcode_defs = {}
mystery_inputs = parse_mystery_input(filename)
mystery_inputs.each do |x|
matches =
which_opcodes_match(x[:before_regs], x[:after_regs], x[:a], x[:b], x[:c])
if opcode_defs.key?(x[:opcode])
opcode_defs[x[:opcode]] &= matches # Array Intersection
else
opcode_defs[x[:opcode]] = matches
end
end
0.upto(20) { opcode_defs = deduce(opcode_defs) }
if opcode_defs.values.select { |x| x.count > 1 }.any?
raise 'Failed to figure out opcodes..'
end
opcode_defs.keys.each { |x| opcode_defs[x] = opcode_defs[x].first }
opcode_defs
end
# Deduce: Loop for opcodes with only one possible meaning and
# remove that meaning from others
# Example Input: { 1 => ["add", "multiply"], 2 => ["multiply"] }
# 1 must be add, because 2 is already multiply...
# Example Output: { 1 => ["add"], 2 => ["multiply"] }
def deduce(opcode_defs)
opcodes_with_one_definition(opcode_defs).each do |opcode|
opcode_defs.keys.reject { |x| x == opcode }.each do |x|
opcode_defs[x].reject! { |y| y == opcode_defs[opcode].first }
end
end
opcode_defs
end
def opcodes_with_one_definition(opcode_defs)
opcode_defs.keys.select { |x| opcode_defs[x].length == 1 }
end
############## MAIN #####################
begin_tests = Time.now
tests
end_tests = Time.now
puts "All tests passed - #{end_tests.to_ms - begin_tests.to_ms}ms"
puts 'How many behave like 3 or more opcodes (Part 1) '
puts part1('input_1.txt')
puts "Let's figure out the opcodes"
opcodes = determine_opcodes('input_1.txt')
puts 'Part2, running program...'
regs = part2('input_2.txt', opcodes)
puts 'Part2: All registers...'
pp regs