-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.php
More file actions
44 lines (34 loc) · 1.17 KB
/
solution.php
File metadata and controls
44 lines (34 loc) · 1.17 KB
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
<?php
$weights = $parents = $children = [];
while ($row = trim(fgets(STDIN))) {
$args = explode(' ', $row);
$weights[$args[0]] = intval(trim($args[1], '()'));
$children[$args[0]] = [];
for ($i = 3; $i < count($args); $i++) {
$parents[rtrim($args[$i], ',')] = $args[0];
$children[$args[0]][] = rtrim($args[$i], ',');
}
}
$part_one = reset($parents);
while (isset($parents[$part_one])) {
$part_one = $parents[$part_one];
}
fwrite(STDOUT, $part_one . PHP_EOL);
function recursive_weight($node)
{
global $children, $weights;
if (!count($children[$node])) {
return $weights[$node];
}
$subweights = [];
foreach ($children[$node] as $child) {
$subweights[$child] = recursive_weight($child);
}
if (count($count_values = array_count_values($subweights)) > 1) {
$misweighed = array_search(array_search(1, $count_values), $subweights);
fwrite(STDOUT, ($weights[$misweighed] + array_search(count($subweights) - 1, $count_values) - $subweights[$misweighed]) . PHP_EOL);
die();
}
return array_sum($subweights) + $weights[$node];
}
recursive_weight($part_one);