-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmas.php
executable file
·130 lines (101 loc) · 2.57 KB
/
xmas.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/php
<?php
$skip = 25;
if(isset($argv[1])) $skip = (int)$argv[1];
echo "--> Preamble size: $skip\n";
$numbers = array_map(function($x) { return (int)$x; }, explode("\n", file_get_contents("/dev/stdin")));
$count = count($numbers);
// part 1
$previous = [];
for($i = 0; $i < $skip; ++$i) {
$previous[$numbers[$i]] = 1;
}
$part1_i = null;
$part1_n = null;
for($i = $skip; $i < $count; ++$i) {
$n = $numbers[$i];
$match_found = false;
foreach($previous as $p => $_ignored) {
$other = $n - $p;
if(isset($previous[$other])) {
$match_found = true;
break;
}
}
if(!$match_found) {
$part1_i = $i;
$part1_n = $n;
break;
}
$old = $numbers[$i - $skip];
if($previous[$old] > 1) {
$previous[$old] -= 1;
} else {
unset($previous[$old]);
}
if(isset($previous[$n])) {
$previous[$n] += 1;
} else {
$previous[$n] = 1;
}
}
if($part1_n === null) {
echo "No suitable numbers found in part1\n";
exit(1);
}
echo "Part1: $part1_n\n";
// part 2
function p2_cut_end($numbers, $sum, $search) {
if($sum === $search) return $numbers;
if($sum < $search) return null;
while(count($numbers) > 2) {
$sum -= array_pop($numbers);
if($sum === $search) return $numbers;
if($sum < $search) return null;
}
return null;
}
function p2_cut_start($numbers, $sum, $search) {
if($sum === $search) return $numbers;
if($sum < $search) return null;
$res = p2_cut_end($numbers, $sum, $search);
if($res !== null) return $res;
while(count($numbers) > 2) {
$sum -= array_shift($numbers);
if($sum < $search) return null;
$res = p2_cut_end($numbers, $sum, $search);
if($res !== null) return $res;
}
return null;
}
function p2(&$numbers, $split_pos) {
$search = $numbers[$split_pos];
$first = array_slice($numbers, 0, $split_pos - 1);
$first_sum = array_reduce($first, function($c, $i) { return $c + $i; }, 0);
$first_result = p2_cut_start($first, $first_sum, $search);
if($first_result !== null) return $first_result;
$second = array_slice($numbers, $split_pos + 1);
$second_sum = array_reduce($first, function($c, $i) { return $c + $i; }, 0);
return p2_cut_start($second_result, $second_sum, $search);
}
function min_max(&$arr, &$min, &$max) {
$min = $max = $arr[0];
$count = count($arr);
for($i = 1; $i < $count; ++$i) {
if($arr[$i] < $min) {
$min = $arr[$i];
} else if($arr[$i] > $max) {
$max = $arr[$i];
}
}
}
$part2 = p2($numbers, $part1_i);
if($part2 === null) {
echo "No suitable numbers found in part2\n";
exit(1);
}
$min;
$max;
min_max($part2, $min, $max);
echo "--> Smallest: $min; Largest: $max\n";
echo "Part2: " . ($min + $max) . "\n";