Browse Source

Complete and add day 5

master
mid-kid 5 years ago
parent
commit
2626200fed
  1. 66
      d05.lua
  2. 1
      d05_input.txt

66
d05.lua

@ -1,5 +1,5 @@
function get_arg(mem, pos, arg) function get_arg(mem, pos, arg)
local param_mode = mem[pos] // (10 ** (arg + 1)) % 10 local param_mode = mem[pos] // (10 ^ (arg + 1)) % 10
if #mem < pos + arg then if #mem < pos + arg then
print(string.format("Error: Opcode too short at end of memory: %d", pos)) print(string.format("Error: Opcode too short at end of memory: %d", pos))
os.exit(false) os.exit(false)
@ -16,7 +16,7 @@ function get_arg(mem, pos, arg)
end end
function get_param(mem, pos, arg) function get_param(mem, pos, arg)
local param_mode = mem[pos] // (10 ** (arg + 1)) % 10 local param_mode = mem[pos] // (10 ^ (arg + 1)) % 10
local arg = get_arg(mem, pos, arg) local arg = get_arg(mem, pos, arg)
if param_mode == 0 then if param_mode == 0 then
return mem[arg] return mem[arg]
@ -34,30 +34,38 @@ function interpret(mem)
local opcode = mem[pos] % 100 local opcode = mem[pos] % 100
if opcode == 1 or opcode == 2 then
local arg1 = get_param(mem, pos, 1)
local arg2 = get_param(mem, pos, 2)
local res = get_arg(mem, pos, 3)
if opcode == 1 then if opcode == 1 then
mem[res] = arg1 + arg2 mem[get_arg(mem, pos, 3)] = get_param(mem, pos, 1) + get_param(mem, pos, 2)
else pos = pos + 4
mem[res] = arg1 * arg2 elseif opcode == 2 then
end mem[get_arg(mem, pos, 3)] = get_param(mem, pos, 1) * get_param(mem, pos, 2)
pos = pos + 4 pos = pos + 4
elseif opcode == 3 then elseif opcode == 3 then
local arg = get_arg(mem, pos, 1)
io.stdout:write("> ") io.stdout:write("> ")
local val = io.stdin:read("n") mem[get_arg(mem, pos, 1)] = io.stdin:read("n")
io.stdout:write(val)
io.stdout:write("\n")
mem[arg] = val
pos = pos + 2 pos = pos + 2
elseif opcode == 4 then elseif opcode == 4 then
io.stdout:write(get_param(mem, pos, 1)) io.stdout:write(get_param(mem, pos, 1))
io.stdout:write("\n")
pos = pos + 2 pos = pos + 2
elseif opcode == 5 then
if get_param(mem, pos, 1) ~= 0 then
pos = get_param(mem, pos, 2) + 1
else
pos = pos + 3
end
elseif opcode == 6 then
if get_param(mem, pos, 1) == 0 then
pos = get_param(mem, pos, 2) + 1
else
pos = pos + 3
end
elseif opcode == 7 then
mem[get_arg(mem, pos, 3)] = get_param(mem, pos, 1) < get_param(mem, pos, 2) and 1 or 0
pos = pos + 4
elseif opcode == 8 then
mem[get_arg(mem, pos, 3)] = get_param(mem, pos, 1) == get_param(mem, pos, 2) and 1 or 0
pos = pos + 4
elseif opcode == 99 then elseif opcode == 99 then
return return
else else
@ -69,27 +77,9 @@ end
file = io.open(arg[1]) file = io.open(arg[1])
memory = {} memory = {}
for num in string.gmatch(file:read(), "(%d+),") do for num in string.gmatch(file:read(), "(-?%d+),?") do
table.insert(memory, tonumber(num)) table.insert(memory, tonumber(num))
end end
file:close() file:close()
memory_p1 = {table.unpack(memory)} interpret(memory)
memory_p1[2] = 12
memory_p1[3] = 2
interpret(memory_p1)
print("Part 1:", memory_p1[1])
-- Brute force
for noun = 0, 99 do
for verb = 0, 99 do
memory_p2 = {table.unpack(memory)}
memory_p2[2] = noun
memory_p2[3] = verb
interpret(memory_p2)
if memory_p2[1] == 19690720 then
print("Part 2:", 100 * noun + verb)
os.exit(true)
end
end
end

1
d05_input.txt

@ -0,0 +1 @@
3,225,1,225,6,6,1100,1,238,225,104,0,1101,69,55,225,1001,144,76,224,101,-139,224,224,4,224,1002,223,8,223,1001,224,3,224,1,223,224,223,1102,60,49,225,1102,51,78,225,1101,82,33,224,1001,224,-115,224,4,224,1002,223,8,223,1001,224,3,224,1,224,223,223,1102,69,5,225,2,39,13,224,1001,224,-4140,224,4,224,102,8,223,223,101,2,224,224,1,224,223,223,101,42,44,224,101,-120,224,224,4,224,102,8,223,223,101,3,224,224,1,223,224,223,1102,68,49,224,101,-3332,224,224,4,224,1002,223,8,223,1001,224,4,224,1,224,223,223,1101,50,27,225,1102,5,63,225,1002,139,75,224,1001,224,-3750,224,4,224,1002,223,8,223,1001,224,3,224,1,223,224,223,102,79,213,224,1001,224,-2844,224,4,224,102,8,223,223,1001,224,4,224,1,223,224,223,1,217,69,224,1001,224,-95,224,4,224,102,8,223,223,1001,224,5,224,1,223,224,223,1102,36,37,225,1101,26,16,225,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1,99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999,1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,1107,677,677,224,102,2,223,223,1006,224,329,1001,223,1,223,1108,677,677,224,1002,223,2,223,1006,224,344,1001,223,1,223,107,226,226,224,1002,223,2,223,1006,224,359,101,1,223,223,1008,226,226,224,102,2,223,223,1005,224,374,1001,223,1,223,1107,226,677,224,1002,223,2,223,1006,224,389,1001,223,1,223,1008,677,226,224,1002,223,2,223,1005,224,404,1001,223,1,223,7,677,226,224,102,2,223,223,1005,224,419,1001,223,1,223,1008,677,677,224,1002,223,2,223,1006,224,434,1001,223,1,223,108,226,226,224,102,2,223,223,1006,224,449,1001,223,1,223,108,677,677,224,102,2,223,223,1006,224,464,1001,223,1,223,107,226,677,224,1002,223,2,223,1005,224,479,101,1,223,223,1108,226,677,224,1002,223,2,223,1006,224,494,1001,223,1,223,107,677,677,224,1002,223,2,223,1006,224,509,101,1,223,223,7,677,677,224,102,2,223,223,1006,224,524,1001,223,1,223,1007,226,677,224,1002,223,2,223,1005,224,539,1001,223,1,223,8,226,677,224,1002,223,2,223,1005,224,554,101,1,223,223,8,677,677,224,102,2,223,223,1005,224,569,101,1,223,223,7,226,677,224,102,2,223,223,1006,224,584,1001,223,1,223,1007,226,226,224,102,2,223,223,1006,224,599,1001,223,1,223,1107,677,226,224,1002,223,2,223,1006,224,614,1001,223,1,223,1108,677,226,224,1002,223,2,223,1005,224,629,1001,223,1,223,1007,677,677,224,102,2,223,223,1006,224,644,1001,223,1,223,108,226,677,224,102,2,223,223,1005,224,659,101,1,223,223,8,677,226,224,1002,223,2,223,1006,224,674,1001,223,1,223,4,223,99,226
Loading…
Cancel
Save