2020 day 8
This commit is contained in:
parent
75d5c9f041
commit
04656699c9
4 changed files with 705 additions and 0 deletions
35
2020/08/part1.js
Normal file
35
2020/08/part1.js
Normal file
|
@ -0,0 +1,35 @@
|
|||
const fs = require('fs');
|
||||
|
||||
const data = fs.readFileSync('input.txt', 'utf-8');
|
||||
const lines = data.split('\n');
|
||||
let instructions = [];
|
||||
|
||||
lines.forEach((line, i) => {
|
||||
t = line.split(' ');
|
||||
instructions[i] = [t[0], parseInt(t[1])]
|
||||
})
|
||||
|
||||
function loop (i, seen, acc) {
|
||||
if (seen.has(i)) {
|
||||
return acc;
|
||||
}
|
||||
seen.add(i);
|
||||
switch (instructions[i][0]) {
|
||||
case 'acc':
|
||||
acc+=instructions[i][1];
|
||||
i ++;
|
||||
break;
|
||||
case 'jmp':
|
||||
i += instructions[i][1];
|
||||
break;
|
||||
case 'nop':
|
||||
i ++;
|
||||
break;
|
||||
default:
|
||||
console.log('something went wrong. got instruction:', instruction[i][0]);
|
||||
}
|
||||
acc = loop(i, seen, acc);
|
||||
return acc;
|
||||
}
|
||||
|
||||
loop(0, new Set(), 0)
|
Loading…
Add table
Add a link
Reference in a new issue