AOC day 2
This commit is contained in:
parent
3eb3276435
commit
2c48fa6efe
|
@ -0,0 +1,39 @@
|
|||
#!/bin/lua
|
||||
|
||||
function computer(a, b)
|
||||
local file = io.open("input")
|
||||
local tape = {}
|
||||
|
||||
for opcode in string.gmatch(file:read("*line"), "%d+") do
|
||||
table.insert(tape, tonumber(opcode))
|
||||
end
|
||||
|
||||
-- restore 1202 program state
|
||||
tape[2], tape[3] = a, b
|
||||
|
||||
local position = 1
|
||||
repeat
|
||||
local args = {tape[position + 1], tape[position + 2], tape[position + 3]}
|
||||
local opcode = tape[position]
|
||||
|
||||
if opcode == 1 then
|
||||
tape[args[3] + 1] = tape[args[1] + 1] + tape[args[2] + 1]
|
||||
elseif opcode == 2 then
|
||||
tape[args[3] + 1] = tape[args[1] + 1] * tape[args[2] + 1]
|
||||
elseif opcode == 99 then
|
||||
return tape[1]
|
||||
end
|
||||
|
||||
position = position + 4
|
||||
until false
|
||||
end
|
||||
|
||||
for noun = 0, 99 do
|
||||
for verb = 0, 99 do
|
||||
local result = computer(noun, verb)
|
||||
if result == 19690720 then
|
||||
print(100 * noun + verb)
|
||||
os.exit()
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1 @@
|
|||
3892
|
|
@ -0,0 +1 @@
|
|||
1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,13,1,19,1,19,10,23,1,23,13,27,1,6,27,31,1,9,31,35,2,10,35,39,1,39,6,43,1,6,43,47,2,13,47,51,1,51,6,55,2,6,55,59,2,59,6,63,2,63,13,67,1,5,67,71,2,9,71,75,1,5,75,79,1,5,79,83,1,83,6,87,1,87,6,91,1,91,5,95,2,10,95,99,1,5,99,103,1,10,103,107,1,107,9,111,2,111,10,115,1,115,9,119,1,13,119,123,1,123,9,127,1,5,127,131,2,13,131,135,1,9,135,139,1,2,139,143,1,13,143,0,99,2,0,14,0
|
|
@ -0,0 +1,29 @@
|
|||
#!/bin/lua
|
||||
|
||||
local file = io.open("input")
|
||||
local tape = {}
|
||||
|
||||
for opcode in string.gmatch(file:read("*line"), "%d+") do
|
||||
table.insert(tape, tonumber(opcode))
|
||||
end
|
||||
|
||||
-- restore 1202 program state
|
||||
tape[2], tape[3] = 12, 2
|
||||
|
||||
local position = 1
|
||||
repeat
|
||||
local args = {tape[position + 1], tape[position + 2], tape[position + 3]}
|
||||
local opcode = tape[position]
|
||||
|
||||
if opcode == 1 then
|
||||
tape[args[3] + 1] = tape[args[1] + 1] + tape[args[2] + 1]
|
||||
elseif opcode == 2 then
|
||||
tape[args[3] + 1] = tape[args[1] + 1] * tape[args[2] + 1]
|
||||
elseif opcode == 99 then
|
||||
print(tape[1])
|
||||
os.exit()
|
||||
end
|
||||
|
||||
position = position + 4
|
||||
until false
|
||||
|
|
@ -0,0 +1 @@
|
|||
1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,13,1,19,1,19,10,23,1,23,13,27,1,6,27,31,1,9,31,35,2,10,35,39,1,39,6,43,1,6,43,47,2,13,47,51,1,51,6,55,2,6,55,59,2,59,6,63,2,63,13,67,1,5,67,71,2,9,71,75,1,5,75,79,1,5,79,83,1,83,6,87,1,87,6,91,1,91,5,95,2,10,95,99,1,5,99,103,1,10,103,107,1,107,9,111,2,111,10,115,1,115,9,119,1,13,119,123,1,123,9,127,1,5,127,131,2,13,131,135,1,9,135,139,1,2,139,143,1,13,143,0,99,2,0,14,0
|
Loading…
Reference in New Issue