function* executeInstructions(instructions) {
let X = 1;
let cycle = 0;
for (const {command, value} of instructions) {
let delay;
function currentState(phase) {
return {cycle, phase, X, command, value, delay};
}
switch (command) {
case "noop": delay = 1; break;
case "addx": delay = 2; break;
default: throw new Error(`unknown command: ${command}`);
}
++cycle;
yield currentState("start");
while (--delay) {
yield currentState("end");
++cycle;
yield currentState("start");
}
switch (command) {
case "noop": break;
case "addx": X += value; break;
default: throw new Error(`unknown command: ${command}`);
}
yield currentState("end");
}
}