-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_13.php
76 lines (65 loc) · 1.76 KB
/
day_13.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
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
<?php
$filePointer = fopen("input_13.txt", "r");
$input = [];
$pairCount = 0;
while (!feof($filePointer) && $line = fgets($filePointer)) {
if ($line == "\r\n") {
$pairCount++;
} else {
$arr = [];
eval("\$arr = $line;");
// $input[$pairCount][] = $arr; // for part 1
$input[] = $arr;
}
}
/* part one
$results = [];
$sum = 0;
foreach ($input as $key => $pair) {
$result = compare($pair, 0);
$results[] = $result;
echo ($result < 1 ? "true" : "false") . " for $key \n";
if ($result < 1) {
$sum += $key+1;
}
}
echo "sum $sum\n";
*/
//var_dump($input[0]);
// part two
$input[] = [[2]];
$input[] = [[6]];
usort($input, "customCompare");
//var_dump($input);
$result = (array_search([[2]], $input) + 1) * (array_search([[6]], $input) + 1);
echo "result $result\n";
function customCompare($one, $two) {
return compare([$one, $two]);
}
// -1, 0, 1
function compare($pair) {
// echo json_encode($pair) . "\n";
$element1 = $pair[0]; // [1,10,[[3,1,0,7,10],[9,10,6,2,1],[3,5],2],[8,[3,0]]]
$element2 = $pair[1]; // [4,0,[],[1,[8,2,7,8,10],9,[10,4,8,3,9]]]
if (is_array($element1) && !is_array($element2)) {
$element2 = [$element2];
} elseif (is_array($element2) && !is_array($element1)) {
$element1 = [$element1];
}
if (!is_array($element1)) {
if ((int)$element1 > (int)$element2) {
return 1;
}
if ((int)$element1 == (int)$element2) {
return 0;
}
return -1;
}
while (count($element1) && count($element2))
{
$left = array_shift($element1);
$right = array_shift($element2);
if ($result = compare([$left, $right])) return $result;
}
return count($element1) - count($element2);
}