-
Notifications
You must be signed in to change notification settings - Fork 0
/
10.js
68 lines (56 loc) · 1.93 KB
/
10.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
68
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import {readInput, sum} from '../../../util.js';
const directoryPath = path.dirname(fileURLToPath(import.meta.url));
const input = await readInput(directoryPath);
const programInstructions = input
.trim()
.split('\n')
.map(line => line.split(' '));
let cycleCount = 0;
let registerValue = 1;
const registerValues = {};
for (const programInstruction of programInstructions) {
const [instruction] = programInstruction;
const instructionValue = Number(programInstruction[1]);
if (instruction === 'noop') {
cycleCount++;
registerValues[cycleCount] = registerValue;
} else {
// Instruction is "addx <value>"
cycleCount += 2;
registerValues[cycleCount - 1] = registerValue;
registerValues[cycleCount] = registerValue;
registerValue += instructionValue;
registerValues[cycleCount + 1] = registerValue;
}
}
// Signal strength = cycle number * value at cycle
const interestingSignalStrengths = [
registerValues[20] * 20,
registerValues[60] * 60,
registerValues[100] * 100,
registerValues[140] * 140,
registerValues[180] * 180,
registerValues[220] * 220,
];
const sumInterestingSignalStrengths = sum(interestingSignalStrengths);
console.log('Sum interesting signal strengths:', sumInterestingSignalStrengths);
function printCrtRow(startIndex, endIndex) {
const crtPixels = [];
let crtCounter = 0;
for (let index = 1; index <= Object.entries(registerValues).slice(startIndex, endIndex).length; index++, crtCounter++) {
const currentRegisterValue = registerValues[index + startIndex];
// Check if CRT counter is inside the sprite
if (currentRegisterValue <= crtCounter + 1 && currentRegisterValue >= crtCounter - 1) {
crtPixels.push('#');
} else {
crtPixels.push('.');
}
}
return crtPixels.join('');
}
console.log('CRT output:');
for (let index = 0; index < Object.entries(registerValues).length; index += 40) {
console.log(printCrtRow(index, index + 40));
}