-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathday08.lua
More file actions
59 lines (52 loc) · 1.52 KB
/
day08.lua
File metadata and controls
59 lines (52 loc) · 1.52 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
local instructions = {}
for line in io.lines(arg[1]) do
local ins, arg = line:match("^(%w+) ([%-%+]+[0-9]+)$")
table.insert(instructions, {ins, arg})
end
function toggle(instruction)
if instruction[1] == "jmp" then
instruction[1] = "nop"
elseif instruction[1] == "nop" then
instruction[1] = "jmp"
end
end
function simulate(instructions, backtrace, pc, acc)
acc = acc or 0
pc = pc or 1
local visits = {}
local machine = {
["nop"] = function(arg) pc = pc + 1 end,
["acc"] = function(arg) acc = acc + arg pc = pc + 1 end,
["jmp"] = function(arg) pc = pc + arg end
}
while visits[pc] == nil and (pc ~= #instructions) do
table.insert(backtrace, {pc, acc})
visits[pc] = true
local ins = instructions[pc][1]
local arg = instructions[pc][2]
machine[ins](arg)
end
table.insert(backtrace, {pc, acc})
return acc
end
function part1(instructions, bc)
return simulate(instructions, bc)
end
function part2(instructions, bc)
for i, b in ipairs(bc) do
if instructions[b[1]][1] ~= "acc" then
local new_bc = {}
toggle(instructions[b[1]])
local acc = simulate(instructions, new_bc, b[1], b[2])
toggle(instructions[b[1]])
if new_bc[#new_bc][1] == #instructions then
return acc
end
end
end
end
local bc = {}
local answer1 = part1(instructions, bc)
local answer2 = part2(instructions, bc)
print(answer1)
print(answer2)