-
Notifications
You must be signed in to change notification settings - Fork 0
/
day8.cr
77 lines (65 loc) · 1.7 KB
/
day8.cr
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
module Day8
REGEXP = %r{
([[:alpha:]]+)
\s
(inc|dec)
\s
(-?\d+)
\s
if
\s
([[:alpha:]]+)
\s
([<>=!]+)
\s
(-?\d+)
}x
extend self
def each
File.read_lines("./day8.txt").each do |line|
if m = line.match(REGEXP)
yield({m[1], m[2], m[3].to_i, m[4], m[5], m[6].to_i})
end
end
end
def solve
registers = Hash(String, Int32).new
instructions = Array(Tuple(String, String, Int32, String, String, Int32)).new
max = 0
each do |line|
object_name, op1, mod, object_cond, op2, cond_val = line
registers[object_name] = 0
registers[object_cond] = 0
instructions << {object_name, op1, mod, object_cond, op2, cond_val}
end
instructions.each do |object_name, op1, mod, object_cond, op2, cond_val|
object_cond = registers[object_cond]
check_result = case op2
when ">="
object_cond >= cond_val
when ">"
object_cond > cond_val
when "!="
object_cond != cond_val
when "=="
object_cond == cond_val
when "<="
object_cond <= cond_val
when "<"
object_cond < cond_val
else
raise op2
end
if check_result
if op1 == "dec"
registers[object_name] -= mod
else
registers[object_name] += mod
end
end
max = registers[object_name] if registers[object_name] > max
end
[registers.values.max, max]
end
end
p Day8.solve