## DEV Community is a community of 764,073 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

# Discussion on: Advent of Code 2020 Solution Megathread - Day 8: Handheld Halting

as soon as i saw this, i remembered the IntCode shenanigans from last year...can't wait til we're forking off 4 copies of this VM to run in parallel or talk to each other or whatever :)

ts solution was pretty compact here, although I got tripped up by the sneaky blank line at the end of the input! I've been doing this for a few years and we're 8 days in, you'd think I'd remember that by now haha.

``````import { SolveFunc } from './types';

export const solve: SolveFunc = (lines) => {
lines = lines.filter((l) => l.length > 0);

for (let i = 0; i < lines.length; i++) {
const line = lines[i];
if (line.startsWith('acc')) {
continue;
}
const chunks = line.split(' ');
const newLine = [swap(chunks[0]), chunks[1]].join(' ');
const oldLine = line;
lines[i] = newLine;

const res = vm(lines);
if (res === -1) {
lines[i] = oldLine;
continue;
}
return res;
}
};

const swap = (op) => {
if (op === 'nop') {
return 'jmp';
} else {
return 'nop';
}
};
const vm = (lines: string[]): number => {
//   console.log(lines);
let acc = 0;
let i = 0;
const seen = new Set();
while (i < lines.length) {
if (seen.has(i)) {
return -1; // ew
}

const [op, arg] = lines[i].split(' ');

if (op === 'nop') {
// pass
i++;
} else if (op === 'acc') {
acc += parseInt(arg);
i++;
} else if (op === 'jmp') {
i += parseInt(arg);
}
}
return acc;
};
``````