Skip to content

Instantly share code, notes, and snippets.

@Splendorr
Created December 4, 2019 02:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Splendorr/ea01d8d76d7a0c6486453179b6cb4ef2 to your computer and use it in GitHub Desktop.
Save Splendorr/ea01d8d76d7a0c6486453179b6cb4ef2 to your computer and use it in GitHub Desktop.
Advent of Code 2019 - Day 2 in Lua
local inspect = require 'inspect'
function table.clone(org)
return {table.unpack(org)}
end
-- INPUTS --------------------
input = {
1,0,0,3,
1,1,2,3,
1,3,4,3,
1,5,0,3,
2,6,1,19,
1,19,10,23,
2,13,23,27,
1,5,27,31,
2,6,31,35,
1,6,35,39,
2,39,9,43,
1,5,43,47,
1,13,47,51,
1,10,51,55,
2,55,10,59,
2,10,59,63,
1,9,63,67,
2,67,13,71,
1,71,6,75,
2,6,75,79,
1,5,79,83,
2,83,9,87,
1,6,87,91,
2,91,6,95,
1,95,6,99,
2,99,13,103,
1,6,103,107,
1,2,107,111,
1,111,9,0,
99,
2,14,0,0
}
input1202 = {
1,12,2,3,
1,1,2,3,
1,3,4,3,
1,5,0,3,
2,6,1,19,
1,19,10,23,
2,13,23,27,
1,5,27,31,
2,6,31,35,
1,6,35,39,
2,39,9,43,
1,5,43,47,
1,13,47,51,
1,10,51,55,
2,55,10,59,
2,10,59,63,
1,9,63,67,
2,67,13,71,
1,71,6,75,
2,6,75,79,
1,5,79,83,
2,83,9,87,
1,6,87,91,
2,91,6,95,
1,95,6,99,
2,99,13,103,
1,6,103,107,
1,2,107,111,
1,111,9,0,
99,
2,14,0,0
}
-- PART 1 ---------------------
function runIntcode(program)
-- print('runIntcode')
local p = program
local readPos = 1
function runLine(readPos)
local opcode = p[readPos]
-- print( 'running opcode: ' .. opcode )
if opcode == 99 then
return
end
local pos1 = p[readPos + 1] + 1
local pos2 = p[readPos + 2] + 1
local dest = p[readPos + 3] + 1
-- print('pos1: ' .. pos1 .. ', pos2: ' .. pos2 .. ', dest: ' .. dest)
if opcode == 1 then
p[dest]= p[pos1] + p[pos2]
elseif opcode == 2 then
p[dest]= p[pos1] * p[pos2]
else
return 'error: invalid opcode'
end
readPos = readPos + 4
-- opcode = p[readPos]
runLine(readPos)
end
runLine(readPos)
return p
end
-- -- TESTS
-- 1,0,0,0,99 becomes 2,0,0,0,99 (1 + 1 = 2).
test1 = {1,0,0,0,99}
print('test1 should be {2,0,0,0,99}: ' .. inspect(runIntcode(test1)))
-- 2,3,0,3,99 becomes 2,3,0,6,99 (3 * 2 = 6).
test2 = {2,3,0,3,99}
print('test2 should be {2,3,0,6,99}: ' .. inspect(runIntcode(test2)))
-- 2,4,4,5,99,0 becomes 2,4,4,5,99,9801 (99 * 99 = 9801).
test3 = {2,4,4,5,99,0}
print('test3 should be {2,4,4,5,99,9801}: ' .. inspect(runIntcode(test3)))
-- 1,1,1,4,99,5,6,0,99 becomes 30,1,1,4,2,5,6,0,99.
test4 = {1,1,1,4,99,5,6,0,99}
print('test4 should be {30,1,1,4,2,5,6,0,99}: ' .. inspect(runIntcode(test4)))
print('input1202 produces: ' .. inspect(runIntcode(input1202)))
-- PART 2 ------------------------------
function testNounVerbOutputProgram(noun, verb, output, program)
local p = table.clone(program)
p[2]= noun
p[3]= verb
-- print(inspect(p))
local calc = runIntcode(p)
-- print('calc: ' .. inspect(calc))
-- print('calc[1]: ' .. calc[1] .. ', output: ' .. output)
return calc[1] == output
end
print('Should be true: ' .. inspect(testNounVerbOutputProgram(12, 2, 4138687, input)))
print('Should be false: ' .. inspect(testNounVerbOutputProgram(20, 2, 4138687, input)))
function findNounAndVerbToProduce(val, program)
local noun = 0
local verb = 0
for i = 0, 99 do
noun = i
for j = 0, 99 do
verb = j
if testNounVerbOutputProgram(noun, verb, val, program) then
return noun, verb
end
end
end
end
local n, v = findNounAndVerbToProduce(19690720, input)
print( 'noun and verb to produce 19690720: ' .. n, v )
local solution = 100 * n + v
print( 'solution: ' .. solution )
@Splendorr
Copy link
Author

Okay I realize lines 149–158 don't need the variable declarations, and that if I DID need them, they should have been local!

for i = 0, 99 do
    for j = 0, 99 do
      if testNounVerbOutputProgram(i, j, val, program) then
        return i, j
      end
    end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment