-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday8.js
76 lines (64 loc) · 1.81 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
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
const fs = require("fs");
const data = fs.readFileSync("day8.txt", "utf8").trim().split("\n");
const day8 = ()=> {
const directions = data[0];
const network = {};
for(let i = 2; i < data.length; i++) {
const node = data[i].replace(/[()]/g, "").split(" = ");
network[node[0]] = {L: node[1].split(", ")[0], R: node[1].split(", ")[1]};
}
let part1 = 0;
let next = "AAA";
for(let i = 0; i <= directions.length; i++) {
if(i == directions.length) {
i=0;
}
part1++;
next = network[next][directions[i]];
if(next === "ZZZ") {
break;
}
}
const nexts = [];
Object.keys(network).forEach(key => {
if(key[2] === "A") {
nexts.push(key);
}
});
let steps = [];
let distances = [];
for(let j = 0; j < nexts.length; j++) {
steps.push(0);
for(let i = 0; i <= directions.length; i++) {
if(i == directions.length) {
i=0;
}
steps[j]++;
nexts[j] = network[nexts[j]][directions[i]];
if(nexts[j][2] === "Z") {
distances.push(steps[j]/directions.length);
break;
}
}
}
return {"part 1": part1, "part 2": LCM(distances)*directions.length};
}
const LCM = (nums) => {
const factors = {};
for(let i = 0; i < nums.length; i++) {
x = 2;
while(x <= nums[i]) {
while(nums[i]%x == 0) {
nums[i] = nums[i]/x;
factors[x] = (factors[x] + 1) || 1;
}
x++;
}
}
let LCM = 1;
Object.keys(factors).forEach(factor => {
LCM = LCM * Number.parseInt(factor)**factors[factor];
});
return LCM;
}
module.exports = day8;