-
-
Save Nimblz/61e98fa67ef18c157e823d6ea4ad19b2 to your computer and use it in GitHub Desktop.
Day2.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local inputString = require(script.Input) | |
local inputStringArray = inputString:split(",") | |
local ADD = 1 | |
local MULTIPLY = 2 | |
local opFunctions = { | |
[ADD] = function(input1, input2) | |
return input1 + input2 | |
end, | |
[MULTIPLY] = function(input1, input2) | |
return input1 * input2 | |
end, | |
} | |
function executeOp(array, opcode, input1Pointer, input2Pointer, returnPointer) | |
local input1 = array[input1Pointer] | |
local input2 = array[input2Pointer] | |
array[returnPointer] = opFunctions[opcode](input1, input2) | |
end | |
function executeProgram(opArray) | |
for i = 1, #opArray, 4 do | |
local opcode = opArray[i] | |
local input1Pointer = opArray[i+1] + 1 | |
local input2Pointer = opArray[i+2] + 1 | |
local returnPointer = opArray[i+3] + 1 | |
if opcode == 99 then | |
return opArray[1] | |
end | |
executeOp(opArray, opcode, input1Pointer, input2Pointer, returnPointer) | |
end | |
end | |
local function getInput(noun, verb) | |
local inputArray = {} | |
for k,v in ipairs(inputStringArray) do | |
table.insert(inputArray, tonumber(v)) | |
end | |
inputArray[2] = noun | |
inputArray[3] = verb | |
return inputArray | |
end | |
-- part 1 solution | |
local inputArray = getInput(12, 2) | |
local output = executeProgram(inputArray) | |
print("Part 1 solution:", output) | |
-- part 2 solution | |
local desiredOutput = 19690720 | |
-- BRUTE FORCE | |
for noun = 0, 99 do | |
for verb = 0, 99 do | |
local output = executeProgram(getInput(noun, verb)) | |
if output == desiredOutput then | |
print("Noun and verb are:", noun, verb) | |
local solution = 100 * noun + verb | |
print("Part 2 solution:", solution) | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
return [[1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,6,19,1,19,5,23,2,13,23,27,1,10,27,31,2,6,31,35,1,9,35,39,2,10,39,43,1,43,9,47,1,47,9,51,2,10,51,55,1,55,9,59,1,59,5,63,1,63,6,67,2,6,67,71,2,10,71,75,1,75,5,79,1,9,79,83,2,83,10,87,1,87,6,91,1,13,91,95,2,10,95,99,1,99,6,103,2,13,103,107,1,107,2,111,1,111,9,0,99,2,14,0,0]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
rip nimblz