adventofcode/2022/10/part2.js

36 lines
787 B
JavaScript

import * as R from "remeda";
export const comsys = () => {
let cycle = 0;
let register = 1;
const scheduled = [];
return (instr) => {
if (instr && instr[0] === "addx") {
scheduled.push(0, instr[1]);
} else if (instr) {
scheduled.push(0);
}
cycle++;
register += scheduled.shift() ?? 0;
return { cycle, register };
};
};
const toPixel = (entry, i) => {
if (entry.register >= i - 1 && entry.register <= i + 1) return "#";
else return ".";
};
export default (instructions) => {
const sys = comsys();
instructions = [...instructions];
return R.pipe(
[sys(), ...R.range(0, 239).map(() => sys(instructions.shift()))],
R.chunk(40),
R.map((line) => line.map(toPixel).join("") + "\n"),
(lines) => lines.join("")
);
};