-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10.js
62 lines (53 loc) · 1.14 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
/**
* @param {string} d
*/
export const part1 = async d => {
const data = d.split('\n').map(e => e.split(' '));
let x = 1;
let cycle = 1;
const cycleTargets = [20, 60, 100, 140, 180, 220];
let signalSum = 0;
while (data.length > 0 && cycleTargets.length) {
const command = data.shift();
cycle += command.length;
if (cycle > cycleTargets[0]) {
signalSum += (cycleTargets.shift() * x);
}
if (command.length == 2) {
x += parseInt(command[1]);
}
}
return signalSum;
};
/**
* @param {string} d
*/
export const part2 = async d => {
const data = d.split('\n').join(' ').split(' ');
let x = 1;
let cycle = 0;
const cycleTargets = [40,80,120,160,200,240];
const output =[''];
const row = [];
const onState = [-1, 0, 1];
cycleTargets.forEach(rowStop => {
row.length = 0;
while (cycle < rowStop) {
const state = (cycle % 40) - x;
const cmd = data.shift();
if (onState.includes(state)) {
row.push('#');
}
else {
row.push('.');
}
if (!isNaN(cmd)) {
x += parseInt(cmd);
}
cycle++;
}
output.push(row.join(''));
});
// Just for reference on how to output
return output.join('\n');
};