adventofcode/2022/10/part2.js

36 lines
787 B
JavaScript
Raw Permalink Normal View History

2022-12-10 22:54:56 +00:00
import * as R from "remeda";
2022-12-10 23:25:50 +00:00
export const comsys = () => {
let cycle = 0;
let register = 1;
const scheduled = [];
2022-12-10 22:54:56 +00:00
2022-12-10 23:25:50 +00:00
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 };
};
};
2022-12-10 22:54:56 +00:00
2022-12-10 23:25:50 +00:00
const toPixel = (entry, i) => {
if (entry.register >= i - 1 && entry.register <= i + 1) return "#";
else return ".";
};
export default (instructions) => {
const sys = comsys();
2022-12-10 22:54:56 +00:00
2022-12-10 23:25:50 +00:00
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("")
);
2022-12-10 22:54:56 +00:00
};