-
Notifications
You must be signed in to change notification settings - Fork 0
/
soln_pixels.js
54 lines (52 loc) · 1.37 KB
/
soln_pixels.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
instructions = input.split("\n");
// part 1
var [total, reg, cyc] = [0, 1, 0];
function nextCyclePartOne() {
cyc += 1;
if ((cyc - 20) % 40 === 0) {
// console.log(`adding ${reg*cyc} to ${total} for cycle ${cyc} and register ${reg}`);
total += reg * cyc;
}
}
instructions.forEach(inst => {
nextCyclePartOne();
if (inst === "noop") {
return;
}
const loadval = parseInt(inst.split(" ")[1]);
nextCyclePartOne();
reg += loadval;
});
console.log(total)
// part 2
var [total, reg, cyc, row, soln] = [0, 1, 0, 0, ""];
function isSpriteActive(cycle, register) {
var cycleIndex = (cycle - 1) % 40;
const overlap = register === cycleIndex || register - 1 === cycleIndex || register + 1 == cycleIndex;
// console.log(`${register} overlaps the pixel ${cycleIndex} on row {parseInt((cycle - 1) / 40)}`);
return overlap;
}
function nextCyclePartTwo() {
if (cyc % 40 === 0) {
// console.log(`next row`);
soln += "\n";
}
cyc += 1;
if (isSpriteActive(cyc, reg)) {
// console.log(`${reg} overlaps the pixel ${(cyc - 1)%40} on row ${parseInt((cyc - 1)/ 40)}`);
soln += "#";
} else {
soln += ".";
}
}
[total, reg, cyc, row, soln] = [0, 1, 0, 0, ""];
instructions.forEach(inst => {
nextCyclePartTwo();
if (inst === "noop") {
return;
}
const loadval = parseInt(inst.split(" ")[1]);
nextCyclePartTwo();
reg += loadval;
});
console.log(soln);