-
Notifications
You must be signed in to change notification settings - Fork 0
/
day19.rb
152 lines (120 loc) · 3.17 KB
/
day19.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
require_relative 'toolbox'
OPS_LOOKUP = {
'addr' => ->(registers, a, b, c) {
registers[c] = registers[a] + registers[b]
registers
},
'addi' => ->(registers, a, b, c) {
registers[c] = registers[a] + b
registers
},
'mulr' => ->(registers, a, b, c) {
registers[c] = registers[a] * registers[b]
registers
},
'muli' => ->(registers, a, b, c) {
registers[c] = registers[a] * b
registers
},
'banr' => ->(registers, a, b, c) {
registers[c] = registers[a] & registers[b]
registers
},
'bani' => ->(registers, a, b, c) {
registers[c] = registers[a] & b
registers
},
'borr' => ->(registers, a, b, c) {
registers[c] = registers[a] | registers[b]
registers
},
'bori' => ->(registers, a, b, c) {
registers[c] = registers[a] | b
registers
},
'setr' => ->(registers, a, b, c) {
registers[c] = registers[a]
registers
},
'seti' => ->(registers, a, b, c) {
registers[c] = a
registers
},
'gtir' => ->(registers, a, b, c) {
registers[c] = (a > registers[b] ? 1 : 0)
registers
},
'gtri' => ->(registers, a, b, c) {
registers[c] = (registers[a] > b ? 1 : 0)
registers
},
'gtrr' => ->(registers, a, b, c) {
registers[c] = (registers[a] > registers[b] ? 1 : 0)
registers
},
'eqir' => ->(registers, a, b, c) {
registers[c] = (a == registers[b] ? 1 : 0)
registers
},
'eqri' => ->(registers, a, b, c) {
registers[c] = (registers[a] == b ? 1 : 0)
registers
},
'eqrr' => ->(registers, a, b, c) {
registers[c] = (registers[a] == registers[b] ? 1 : 0)
registers
},
}
class CPU
attr_reader :registers
def initialize(registers)
@registers = registers
end
def run_program(lines)
@instruction_pointer_register = lines.shift.split(' ').last.to_i
@instructions = lines.map {|l| Instruction.new(l) }
@instruction_pointer = @registers[@instruction_pointer_register]
loop do
break if @instructions.size <= @instruction_pointer
@registers[@instruction_pointer_register] = @instruction_pointer
@instructions[@instruction_pointer].execute(@registers)
@instruction_pointer = @registers[@instruction_pointer_register]
@instruction_pointer += 1
end
end
def to_s
"r: #{@registers.inspect}, ipr: #{@instruction_pointer_register}, ip: #{@instruction_pointer}"
end
end
class Instruction
def initialize(args)
values = args.split(' ')
@op_name = values.shift
@a, @b, @c = *values.map(&:to_i)
@op = OPS_LOOKUP[@op_name]
end
def execute(registers)
@op.call(registers, @a, @b, @c)
end
def to_s
"#{@op_name} #{@a} #{@b} #{@c}"
end
end
def run_tests
cpu = CPU.new([0,0,0,0,0,0])
cpu.run_program(raw_lines('data/day19_test_program.txt'))
puts cpu.registers.inspect
end
def part1
cpu = CPU.new([0,0,0,0,0,0])
cpu.run_program(raw_lines('data/day19_production_program.txt'))
cpu.registers.first
end
def part2
return 'calculated manually'
end
run_tests
puts "Part 1: What value is left in register 0 when the background process halts?"
puts "Answer: #{part1}"
puts "What value is left in register 0 when this new background process halts?"
puts "Answer: #{part2}"