-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday23.rb
47 lines (37 loc) · 1.03 KB
/
day23.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
require 'pry'
# Part A
# registers = { "a" => 0, "b" => 0 }
# Part B
registers = { "a" => 1, "b" => 0 }
instructions = File.open('day23input.txt').readlines.map(&:strip)
i = 0
max_i = instructions.length - 1
def parse_offset(offset)
match = offset.match(/(\+|\-)(\d+)/)
mod = (match[1] == "-") ? -1 : 1
mod*match[2].to_i
end
while i >= 0 && i <= max_i do
instruction = instructions[i]
offset = 1
if match = instruction.match(/inc (.+)/)
registers[match[1]] += 1
elsif match = instruction.match(/hlf (.+)/)
registers[match[1]] /= 2
elsif match = instruction.match(/tpl (.+)/)
registers[match[1]] *= 3
elsif match = instruction.match(/jio (.+)\, (.+)/)
if registers[match[1]] == 1
offset = parse_offset(match[2])
end
elsif match = instruction.match(/jie (.+)\, (.+)/)
if registers[match[1]] % 2 == 0
offset = parse_offset(match[2])
end
elsif match = instruction.match(/jmp (.+)/)
offset = parse_offset(match[1])
end
puts registers.inspect
i += offset
end
puts registers.inspect