-
Notifications
You must be signed in to change notification settings - Fork 0
/
10.ts
36 lines (31 loc) · 807 Bytes
/
10.ts
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
import "./utils/helpers.ts";
let input = await Deno.readTextFile("10.txt");
const data = input.trim().lines().trim();
const result = data.reduce(
(av, cv) => {
const [instr, arg] = cv.split(" ");
let addxCounter = 0;
let instrDone = false;
while (!instrDone) {
// start of instr
if (instr === "noop") {
instrDone = true;
} else if (instr === "addx") {
if (addxCounter === 1) instrDone = true;
addxCounter++;
}
av.cycle++;
// end of instr
if ([20, 60, 100, 140, 180, 220].includes(av.cycle)) {
av.signalSum += av.cycle * av.x;
}
}
// after instr
if (addxCounter) {
av.x += parseInt(arg, 10);
}
return av;
},
{ x: 1, cycle: 0, signalSum: 0 }
);
console.log(result.signalSum);