-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday12_part1.rb
45 lines (40 loc) · 959 Bytes
/
day12_part1.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
$registers = {"a" => 0, "b" => 0, "c" => 0, "d" => 0}
def process(instructions)
current_index = 0
while (current_index < instructions.length)
instr = instructions[current_index]
if (instr.match(/cpy ([0-9a-d]+) ([a-d])/))
$registers[$2] = value($1)
elsif(instr.match(/inc ([a-d])/))
$registers[$1] += 1
elsif(instr.match(/dec ([a-d])/))
$registers[$1] -= 1
elsif(instr.match(/jnz ([0-9a-d]+) (-*[0-9]+)/) )
if (value($1) != 0)
current_index += $2.to_i
#puts current_index
next
end
else
raise instr
end
current_index += 1
#puts current_index
end
puts "Finished processing, value in 'a' => #{$registers['a']}"
end
def value(input)
if (input.match(/[a-d]/))
$registers[input]
else
input.to_i
end
end
test = "cpy 41 a
inc a
inc a
dec a
jnz a 2
dec a"
process(test.split("\n"))
process(File.new("day12_input.txt").readlines.map{|l|l.strip})