From 9ac24ee0fa0d5e58250530fa3e94bd1427be5281 Mon Sep 17 00:00:00 2001 From: Aelita4 Date: Sat, 10 Dec 2022 17:38:47 +0100 Subject: [PATCH] 10_2 --- day10/index.js | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/day10/index.js b/day10/index.js index 76319c8..b2f6fa8 100644 --- a/day10/index.js +++ b/day10/index.js @@ -2,9 +2,17 @@ import { readFileSync } from "fs"; 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 cycle = 0; -let strengthSum = 0; input.split('\n').forEach(line => { const instruction = line.split(' '); @@ -12,17 +20,18 @@ input.split('\n').forEach(line => { switch(instruction[0]) { case "noop": cycle++; - if((cycle + 20) % 40 === 0) strengthSum += (cycle * xRegister); + drawCRT(cycle, xRegister); break; case "addx": cycle++; - if((cycle + 20) % 40 === 0) strengthSum += (cycle * xRegister); + drawCRT(cycle, xRegister); cycle++; - - if((cycle + 20) % 40 === 0) strengthSum += (cycle * xRegister); + drawCRT(cycle, xRegister); xRegister += parseInt(instruction[1]); break; } + + }); -console.log(strengthSum) \ No newline at end of file +console.log(display) \ No newline at end of file