-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
80 lines (61 loc) · 1.91 KB
/
index.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
76
77
78
79
80
'use strict';
const fs = require('node:fs');
const input = process.argv.length > 2 ? process.argv[2] : 'input';
const text = String(fs.readFileSync(`./${input}.txt`));
const rows = text.split('\n');
const dirs = rows[0].split('');
const nodes = {};
for (let r = 2; r < rows.length; r++) {
const [ tmp, id, L, R ] = rows[r].match(/([0-9A-Z]{3}) = \(([0-9A-Z]{3}), ([0-9A-Z]{3})/);
nodes[id] = {L, R};
}
function runsTo(start, endings) {
let d = 0;
let pos = start;
let runs = 0;
do {
if (d == dirs.length) d = 0;
pos = nodes?.[pos]?.[dirs[d++]];
runs++;
} while (pos && !endings.includes(pos));
return runs;
}
// Round 1
console.log('Round 1:');
console.log(runsTo('AAA', ['ZZZ']));
// Round 2
/*
const ghostNodes = Object
.keys(nodes)
.filter(n => n.endsWith('A'));
let d = 0;
let runs = 0;
do {
runs++;
if (d == dirs.length) d = 0;
let dir = dirs[d++];
for (let g = 0; g < ghostNodes.length; g++) {
const newPos = nodes[ghostNodes[g]][dir];
if (!newPos) console.error(`${ghostNodes[g]} as no new pos ${dir}`) && process.exit();
//console.log(`${runs}: ${ghostNodes[g]} moves ${dir} to ${newPos}`);
ghostNodes[g] = newPos;
}
if (runs > 100) {
//process.exit();
}
} while (ghostNodes.filter(n => n.endsWith('Z')).length !== ghostNodes.length);
*/
const startNodes = Object.keys(nodes).filter((n) => n.endsWith('A'));
const endNodes = Object.keys(nodes).filter((n) => n.endsWith('Z'));
function leastCommonMultiple(runs) {
const ggT = (a, b) => (!b ? a : ggT(b, a % b));
const kgV = (a, b) => (a * b) / ggT(a, b);
return runs.reduce(
(multiple, num) => kgV(multiple, num),
Math.min(...runs) // start with min values
);
}
const runsToEnd = startNodes.map((n) => runsTo(n, endNodes));
const result2 = leastCommonMultiple(runsToEnd);
console.log('Round 2:');
console.log(result2);