-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart2.js
67 lines (53 loc) · 1.52 KB
/
part2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const { appendFileSync } = require("fs");
const { getInputString } = require("../utils/getInputString.js");
const sum = arr => arr.reduce((acc, cur) => {
acc += cur;
return acc;
}, 0);
const file = getInputString(10, false);
const instructions = file.split("\n");
const Run = () => {
let x = 1;
let instructionNum = 0;
let timeToWait = 0;
let valueToAddOnceDoneWaiting = 0;
let doneWithInstructions = false;
for (let cycle = 1; !doneWithInstructions; cycle++) {
if (timeToWait === 0) {
x += valueToAddOnceDoneWaiting;
if (instructionNum === instructions.length - 1) {
doneWithInstructions = true;
}
const instruction = instructions[instructionNum];
instructionNum++;
if (instruction === "noop") {
valueToAddOnceDoneWaiting = 0;
timeToWait += 1;
} else {
valueToAddOnceDoneWaiting = parseInt(instruction.split(" ")[1]);
timeToWait += 2;
}
}
timeToWait--;
draw(cycle, x);
}
}
const getRowToDraw = (cycle, rowToDraw = 0) => {
if(cycle > 40) {
return getRowToDraw(cycle - 40, rowToDraw + 1);
}
return [rowToDraw, cycle];
}
const screen = [[],[],[],[],[],[]];
const draw = (cycle, x) => {
const [row, column] = getRowToDraw(cycle)
const spriteWindow = [x, x+1, x+2];
const lit = spriteWindow.includes(column);
screen[row][column] = lit ? "#" : ".";
}
Run()
for(const row of screen) {
appendFileSync("test.txt", row.join(''));
appendFileSync("test.txt", '\n');
}
appendFileSync("test.txt", '\n');