Advent of code - Day 8
Quentin Ménoret
Posted on December 9, 2020
Are you participating in the Advent of code this year?
If you don't know what the advent of code is, it's a website where you'll find a daily challenge (every day it gets harder). It's a really fun event, you should participate!
I try to solve the exercises using either JavaScript or TypeScript and will share my solutions daily (with one day delay so no one can cheat!). I only share the solution for the second part.
When I saw the exercise for day #8, I really though "oh boy, this one is going to be hardcore". I thought we'd need to implement a runtime for their weird assembly-like language. Lucky for us, this was way easier than what I assumed!
I basically just wrote a function that determines if the program exits successfully or not, and then run it agains every possible version of the assembly-like code. Here is what it looks like:
const SUCCESS = "SUCCESS";
const ERROR = "ERROR";
// Runs the program, and return SUCCESS or ERROR
// Depending on whether or not it finished (line number > code.length)
function runProgram(code) {
// If a line is processed 2 times, it's an error
const alreadyProcessed = [];
let global = 0;
let currentLine = 0;
while (true) {
if (alreadyProcessed[currentLine] === true) return { global, status: ERROR, alreadyProcessed };
if (code[currentLine] === undefined)
return { global, status: SUCCESS, alreadyProcessed };
alreadyProcessed[currentLine] = true;
const [inst, argument] = code[currentLine];
switch (inst) {
case "acc":
global += parseInt(argument, 10);
currentLine += 1;
break;
case "jmp":
currentLine += parseInt(argument, 10);
break;
case "nop":
currentLine += 1;
break;
default:
throw new Error(inst);
}
}
}
// Let's just bruteforce, and run the program changing any
// line that is a nop or a jmp to find which one is corrupted
input.forEach((_value, index) => {
const code = [...input];
const [inst, argument] = code[index];
if (inst === "jmp") code[index] = ["nop", argument];
else if (inst === "nop") code[index] = ["jmp", argument];
const altResult = runProgram(code);
if (altResult.status === "SUCCESS") console.log(altResult);
});
Feel free to share your solution in the comments!
Photo by Markus Spiske on Unsplash
Posted on December 9, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.