Skip to content

Instantly share code, notes, and snippets.

@MJGTwo
Created December 3, 2019 19:04
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 MJGTwo/2508c12fae89b8ecfb904b6eb30150dd to your computer and use it in GitHub Desktop.
Save MJGTwo/2508c12fae89b8ecfb904b6eb30150dd to your computer and use it in GitHub Desktop.
AOC 2019 answer
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