function get_arg(mem, pos, arg) local param_mode = mem[pos] // (10 ** (arg + 1)) % 10 if #mem < pos + arg then print(string.format("Error: Opcode too short at end of memory: %d", pos)) os.exit(false) end local arg = mem[pos + arg] if param_mode == 0 then arg = arg + 1 if #mem < arg then print(string.format("Error: Memory offset out of bounds: %d", arg)) os.exit(false) end end return arg end function get_param(mem, pos, arg) local param_mode = mem[pos] // (10 ** (arg + 1)) % 10 local arg = get_arg(mem, pos, arg) if param_mode == 0 then return mem[arg] end return arg end function interpret(mem) local pos = 1 while true do if #mem < pos then print(string.format("Instruction pointer ran off of memory")) os.exit(false) end 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 mem[res] = arg1 + arg2 else mem[res] = arg1 * arg2 end pos = pos + 4 elseif opcode == 3 then local arg = get_arg(mem, pos, 1) io.stdout:write("> ") local val = io.stdin:read("n") io.stdout:write(val) io.stdout:write("\n") mem[arg] = val pos = pos + 2 elseif opcode == 4 then io.stdout:write(get_param(mem, pos, 1)) pos = pos + 2 elseif opcode == 99 then return else print(string.format("Error: Invalid opcode %d at %d", opcode, pos - 1)) os.exit(false) end end end file = io.open(arg[1]) memory = {} for num in string.gmatch(file:read(), "(%d+),") do table.insert(memory, tonumber(num)) end file:close() memory_p1 = {table.unpack(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