-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2020_day8.lua
More file actions
61 lines (58 loc) · 1.27 KB
/
Copy path2020_day8.lua
File metadata and controls
61 lines (58 loc) · 1.27 KB
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
local instructions = {}
for v in getinput():gmatch("[^\n]+") do
local op, value = v:match("(%S+) (%S+)")
instructions[#instructions+1] = {op = op, value = tonumber(value)}
end
local acc = 0
local visited = {}
local ip = 1
while not visited[ip] do
visited[ip] = true
local line = instructions[ip]
if line.op == "acc" then
acc = acc + line.value
ip = ip + 1
elseif line.op == "nop" then
ip = ip + 1
elseif line.op == "jmp" then
ip = ip + line.value
end
end
print(acc) -- part 1
local versions = {}
for i = 1, #instructions do
local copy = {}
for j = 1, #instructions do
copy[j] = {op = instructions[j].op, value = instructions[j].value}
end
local changed = false
if copy[i].op == "jmp" then
copy[i].op = "nop"
changed = true
elseif copy[i].op == "nop" then
copy[i].op = "jmp"
changed = true
end
if changed then
versions[#versions + 1] = {instructions = copy, acc = 0, ip = 1}
end
end
while true do
for i = #versions, 1, -1 do
local v = versions[i]
local line = v.instructions[v.ip]
if not line then
print(v.acc) -- part 2
return
else
if line.op == "acc" then
v.acc = v.acc + line.value
v.ip = v.ip + 1
elseif line.op == "nop" then
v.ip = v.ip + 1
elseif line.op == "jmp" then
v.ip = v.ip + line.value
end
end
end
end