This commit is contained in:
Aelita4 2022-12-10 17:38:47 +01:00
parent 2a025f9c1d
commit 9ac24ee0fa
Signed by: Aelita4
GPG Key ID: E44490C2025906C1
1 changed files with 15 additions and 6 deletions

View File

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