-
Notifications
You must be signed in to change notification settings - Fork 0
/
d12.php
46 lines (37 loc) · 895 Bytes
/
d12.php
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
<?php
$json = json_decode(file_get_contents('input_d12.txt'));
function get_sum($obj)
{
if (is_int($obj)) return $obj;
$sum = 0;
if (is_array($obj)) {
foreach ($obj as $key => $value) {
$sum += get_sum($value);
}
} else if (is_object($obj)) {
foreach ($obj as $prop => $value) {
$sum += get_sum($value);
}
}
return $sum;
}
var_dump(get_sum($json));
function get_sum_red($obj)
{
if (is_int($obj)) return $obj;
$sum = 0;
if (is_array($obj)) {
foreach ($obj as $key => $value) {
$sum += get_sum_red($value);
}
} else if (is_object($obj)) {
foreach ($obj as $prop => $value) {
if ($value === 'red') {
return 0;
}
$sum += get_sum_red($value);
}
}
return $sum;
}
var_dump(get_sum_red($json));