-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday8_2.js
75 lines (66 loc) · 1.71 KB
/
day8_2.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
69
70
71
72
73
74
75
const { parse } = require('path');
const lineReader = require('readline').createInterface({
input: require('fs').createReadStream('input.txt'),
});
let instructions = '';
let lineNr = 0;
let map = new Map();
const startingPoints = [];
function findLCD(numbers) {
let result = numbers[0];
for (let i = 1; i < numbers.length; i++) {
result = findLCM(result, numbers[i]);
}
return result;
}
function findLCM(a, b) {
const max = Math.max(a, b);
const min = Math.min(a, b);
// Calculate LCM using the formula: LCM(a, b) = (|a * b|) / GCD(a, b)
return (max / findGCD(max, min)) * min;
}
function findGCD(a, b) {
// Use Euclidean algorithm to find GCD
while (b !== 0) {
const temp = b;
b = a % b;
a = temp;
}
return a;
}
lineReader.on('line', function (line) {
if (lineNr === 0) {
instructions = line;
} else if (lineNr > 1) {
const parts = line.split(' = ');
const point = parts[0];
const left = parts[1].split(', ')[0].substring(1);
const right = parts[1].split(', ')[1].slice(0, -1);
map.set(point, { left, right });
if (point.endsWith('A')) {
startingPoints.push(point);
}
}
lineNr++;
});
lineReader.on('close', function () {
const n = instructions.length;
let currentPoints = startingPoints;
const counts = new Map();
for (let j = 0; j < currentPoints.length; j++) {
let point = currentPoints[j];
let i = 0;
while (!point.endsWith('Z')) {
const direction = instructions[i % n];
if (direction === 'L') {
point = map.get(point).left;
} else {
point = map.get(point).right;
}
i++;
}
counts.set(j, i);
}
const values = Array.from(counts.values());
console.log(findLCD(values));
});