-
Notifications
You must be signed in to change notification settings - Fork 0
/
day10.js
31 lines (26 loc) · 826 Bytes
/
day10.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
import { readFile } from './helpers.js';
const data = readFile(10)
.split('\n')
.map((line) => line.split(' '));
const noop = (acc) => [...acc, acc.at(-1)];
const addx = (acc, [_, arg]) => [
...[...acc, acc.at(-1)],
acc.at(-1) + Number(arg),
];
const ops = {
noop, // return a copy of the prev value
addx, // return a copy of the [prev value, prev value + arg]
};
const cycles = data
.reduce((acc, [op, arg]) => ops[op](acc, [op, arg]), [1])
.slice(0, -1);
const part1 = [20, 60, 100, 140, 180, 220]
.map((i) => cycles[i - 1] * i)
.reduce((a, b) => a + b);
console.log(part1);
// Part 2
cycles.forEach((op, i) => {
const char = [op, op + 1, op + 2].includes((i % 40) + 1) ? 'X' : '.';
process.stdout.write(char);
if ((i + 1) % 40 === 0) process.stdout.write('\n');
});