advent-of-code-2022/day10/index.js

37 lines
778 B
JavaScript
Raw Permalink Normal View History

2022-12-10 16:17:45 +00:00
import { readFileSync } from "fs";
const input = readFileSync("instructions.txt", "utf-8");
2022-12-10 16:38:47 +00:00
let display = "";
function drawCRT(cycle, pointer) {
if((cycle - 1) % 40 === 0) display += "\n"
if(Math.abs(((cycle - 1) % 40) - pointer) <= 1) display += "#";
else display += ".";
}
2022-12-10 16:17:45 +00:00
let xRegister = 1;
let cycle = 0;
input.split('\n').forEach(line => {
const instruction = line.split(' ');
switch(instruction[0]) {
case "noop":
cycle++;
2022-12-10 16:38:47 +00:00
drawCRT(cycle, xRegister);
2022-12-10 16:17:45 +00:00
break;
case "addx":
cycle++;
2022-12-10 16:38:47 +00:00
drawCRT(cycle, xRegister);
2022-12-10 16:17:45 +00:00
cycle++;
2022-12-10 16:38:47 +00:00
drawCRT(cycle, xRegister);
2022-12-10 16:17:45 +00:00
xRegister += parseInt(instruction[1]);
break;
}
2022-12-10 16:38:47 +00:00
2022-12-10 16:17:45 +00:00
});
2022-12-10 16:38:47 +00:00
console.log(display)