-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc.cr
82 lines (76 loc) · 1.42 KB
/
aoc.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
78
79
80
81
82
def main
filename = ARGV.size > 0 ? ARGV[0] : "input.txt"
ops = File
.read(filename)
.lines
.map { |line| line.split(" ") }
puts run(ops)
puts run(ops, 1)
end
def run(ops : Array(Array(String)), c_init = 0) : Int32
a, b, c, d, pc = 0, 0, c_init, 0, 0
while pc < ops.size
op = ops[pc]
pc += 1
if op[0] == "inc"
if op[1] == "a"
a += 1
elsif op[1] == "b"
b += 1
elsif op[1] == "c"
c += 1
elsif op[1] == "d"
d += 1
end
elsif op[0] == "dec"
if op[1] == "a"
a -= 1
elsif op[1] == "b"
b -= 1
elsif op[1] == "c"
c -= 1
elsif op[1] == "d"
d -= 1
end
elsif op[0] == "cpy"
val =
if op[1] == "a"
a
elsif op[1] == "b"
b
elsif op[1] == "c"
c
elsif op[1] == "d"
d
else
op[1].to_i
end
if op[2] == "a"
a = val
elsif op[2] == "b"
b = val
elsif op[2] == "c"
c = val
elsif op[2] == "d"
d = val
end
elsif op[0] == "jnz"
val =
if op[1] == "a"
a
elsif op[1] == "b"
b
elsif op[1] == "c"
c
elsif op[1] == "d"
d
else
op[1].to_i
end
if val != 0
pc += op[2].to_i - 1 # -1 due to pc += 1 above
end
end
end
a
end