-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day07.js
95 lines (76 loc) · 2.72 KB
/
Day07.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const Day07 = {
parseList: (input) => {
let rows = input.split(/\n+/),
output = [];
for (let i = 0; i < rows.length; i++) {
let matches = rows[i].match(/^([a-z]+) \((\d+)\)/);
let disc = {
name: matches[1],
weight: +matches[2]
}
if (rows[i].match(/->/)) {
disc.children = rows[i].match(/-> (.*)/)[1].split(/,\s+/);
}
output.push(disc);
}
return output;
},
findRoot: (tower, currDisc=null) => {
currDisc = currDisc || tower[0];
for (let i = 0; i < tower.length; i++) {
if (tower[i].children && tower[i].children.indexOf(currDisc.name) >= 0) {
return Day07.findRoot(tower, tower[i]);
}
}
return currDisc;
},
findParent: (disc, tower) => {
for (let i = 0; i < tower.length; i++) {
if (tower[i].children && tower[i].children.indexOf(disc.name) >= 0) {
return tower[i];
}
}
return disc;
},
findDisc: (name, tower) => {
return (tower.filter((d) => d.name === name) || [{}])[0];
},
setTotalWeights: (disc, tower) => {
disc.totalWeight = disc.weight;
if (disc.children) {
for (let i = 0; i < disc.children.length; i++) {
let child = Day07.findDisc(disc.children[i], tower);
child.totalWeight = Day07.setTotalWeights(child, tower);
disc.totalWeight += child.totalWeight;
}
}
return disc.totalWeight;
},
findUnbalancedChild: (root, tower) => {
let children = (root.children || []).map((n) => Day07.findDisc(n, tower)),
maxWeight = children.reduce((a, c) => Math.max(a, c.totalWeight), 0),
minWeight = children.reduce((a, c) => Math.min(a, c.totalWeight), 0xffffffff),
withMax = children.filter((c) => c.totalWeight === maxWeight),
withMin = children.filter((c) => c.totalWeight === minWeight);
if (withMax.length === withMin.length) {
return root;
}
let unbalanced = (withMax.length === 1 ? withMax : withMin)[0];
return Day07.findUnbalancedChild(unbalanced, tower);
},
Part1: (input) => {
return Day07.findRoot(Day07.parseList(input)).name;
},
Part2: (input) => {
let tower = Day07.parseList(input),
root = Day07.findRoot(tower);
Day07.setTotalWeights(root, tower);
let unbalanced = Day07.findUnbalancedChild(root, tower),
parentOfUnbalanced = Day07.findParent(unbalanced, tower),
firstSibling = parentOfUnbalanced.children
.map((n) => Day07.findDisc(n, tower))
.filter((c) => c.totalWeight !== unbalanced.totalWeight)[0],
neededWeight = unbalanced.weight - (unbalanced.totalWeight - firstSibling.totalWeight);
return neededWeight;
}
};