-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday8.js
38 lines (30 loc) · 1.24 KB
/
day8.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
import { readFileSync } from 'fs';
const [instructions, network] = readFileSync('../input/day8.txt').toString().split('\n\n');
const nodes = network.split('\n').map(line => line.replaceAll(/[\(\)]/g,'').split(' = '));
const nodeMap = nodes.reduce((acc, cur) => {
acc[cur[0]] = cur[1].split(', ');
return acc;
}, {});
const solveSteps = (endCondition) => (position) => {
let step;
for (step = 0; !endCondition(position); step++) {
const instruction = instructions.charAt(step % instructions.length);
const options = nodeMap[position];
position = options[instruction === 'R' ? 1 : 0];
}
return step;
}
const part1Solver = solveSteps(position => position === 'ZZZ');
const part1 = part1Solver('AAA');
console.log(`part1: ${part1}`);
const greatestCommonDivisor = (a, b) => {
return b ? greatestCommonDivisor(b, a % b) : a;
}
const leastCommonMultiple = (a, b) => {
return a * b / greatestCommonDivisor(a, b);
}
let startPositions = Object.keys(nodeMap).filter(key => key.endsWith('A'));
const part2Solver = solveSteps(position => position.endsWith('Z'));
const stepsForEacStart = startPositions.map(position => part2Solver(position));
const lcm = stepsForEacStart.reduce((a, b) => leastCommonMultiple(a, b));
console.log(`part2: ${lcm}`);