-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday12_part2.rb
80 lines (68 loc) · 1.64 KB
/
day12_part2.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
$registers = {:a => 0, :b => 0, :c => 1, :d => 0}
class Instruction
def initialize(type, value1, value2 = nil)
@type = type
@value1 = value1.match(/[a-d]/) ? value1.to_sym : value1.to_i
@value2 = (value2.match(/[a-d]/) ? value2.to_sym : value2.to_i) if value2 != nil
@register_values = $registers.keys
end
def process
if (@type == :cpy)
$registers[@value2] = value(@value1)
elsif (@type == :inc)
$registers[@value1] += 1
elsif (@type == :dec)
$registers[@value1] -= 1
elsif (@type == :jnz)
if (value(@value1) != 0)
return @value2
end
end
return 1
end
def value(input)
case input
when Symbol
$registers[input]
else
input
end
end
end
class Processor
attr_reader :instructions
def parse(lines)
@instructions = []
lines.each do |instr|
if (instr.match(/cpy ([0-9a-d]+) ([a-d])/))
@instructions << Instruction.new(:cpy, $1, $2)
elsif(instr.match(/inc ([a-d])/))
@instructions << Instruction.new(:inc, $1)
elsif(instr.match(/dec ([a-d])/))
@instructions << Instruction.new(:dec, $1)
elsif(instr.match(/jnz ([0-9a-d]+) (-*[0-9]+)/) )
@instructions << Instruction.new(:jnz, $1, $2)
end
end
end
def process
current_index = 0
while (current_index < @instructions.length)
instr = @instructions[current_index]
current_index += instr.process
end
puts "Finished processing, value in 'a' => #{$registers[:a]}"
end
end
test = "cpy 41 a
inc a
inc a
dec a
jnz a 2
dec a"
p = Processor.new
p.parse(test.split("\n"))
p.process
p = Processor.new
p.parse(File.new("day12_input.txt").readlines.map{|l|l.strip})
p.process