Created
December 3, 2019 19:04
-
-
Save MJGTwo/2508c12fae89b8ecfb904b6eb30150dd to your computer and use it in GitHub Desktop.
AOC 2019 answer
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
const parse = text => text.split(",").map(el => +el); | |
let backupMemory = parse(input); | |
const optCode = number => (a, b) => | |
({ | |
1: a + b, | |
2: a * b, | |
99: null | |
}[number]); | |
const readFromMemory = (memory, location) => memory[location]; | |
const writeToMemory = (memory, location, value) => (memory[location] = value); | |
const executeInstruction = (memory, place) => | |
writeToMemory( | |
memory, | |
memory[place + 3], | |
optCode(memory[place])( | |
readFromMemory(memory, memory[place + 1]), | |
readFromMemory(memory, memory[place + 2]) | |
) | |
); | |
runP1 = (noun, verb) => { | |
let [first, second, third, ...instructions] = backupMemory.slice(0); | |
let instructionSet = [first, noun, verb, ...instructions]; | |
console.log(instructionSet); | |
instructionSet.map((addr, index) => | |
index % 4 === 0 ? executeInstruction(instructionSet, index) : null | |
); | |
console.log(instructionSet); | |
return instructionSet; | |
}; | |
runP2 = (find = 19690720) => { | |
for ( | |
noun = 0, verb = 0; | |
noun <= 99 && verb <= 99; | |
noun >= 99 ? (verb++, (noun = 0)) : noun++ | |
) { | |
check = runP1(noun, verb); | |
if (check[0] === find) { | |
console.log(noun, verb, 100 * noun + verb); | |
break; | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment