10_2
This commit is contained in:
parent
2a025f9c1d
commit
9ac24ee0fa
|
@ -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)
|
Loading…
Reference in New Issue