class VM {
x = 1;
signal = 0;
cycles = 0;
instructions = [];
delayed = {};
ptr = 0;
load(input) {
this.instructions = input.split("\n").map((l) => {
const tokens = l.split(" ");
if (tokens[0] === "noop") {
return tokens;
}
return [tokens[0], parseInt(tokens[1])];
});
}
step() {
this.cycles++;
if ("addx" === this.instructions[this.ptr][0]) {
this.cycles++;
this.x += this.instructions[this.ptr][1];
}
this.ptr++;
}
run() {
this.cycles = 0;
for (let i = 0; i < this.instructions.length; i++) {
this.step();
}
return this.x;
}
}