-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9.php
54 lines (43 loc) · 1.29 KB
/
9.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
<?php
$input = file("inputs/9.txt");
//$input = file("inputs/9-test.txt");
$sumA= 0;
$sumB= 0;
foreach($input as $line){
$sequenzes = [];
$sequenzes[] = explode(" ", trim($line));
$currentSequenz = $sequenzes[0];
// var_dump($currentSequenz);
while(!allNull($currentSequenz)){
$diffs = [];
for($index = 0; $index < count($currentSequenz)-1; $index++){
$diffs[] = $currentSequenz[$index+1] - $currentSequenz[$index];
}
$sequenzes[] = $diffs;
// var_dump($diffs);
$currentSequenz = $diffs;
}
$nextValue = 0;
for($index = count($sequenzes)-2; $index >= 0; $index--){
$nextValue += end($sequenzes[$index]);
}
$prevValue = 0;
for($index = count($sequenzes)-2; $index >= 0; $index--){
$prevValue = $sequenzes[$index][0] - $prevValue;
}
// var_dump($nextValue);
// return;
$sumA += $nextValue;
$sumB += $prevValue;
}
var_dump($sumA);
var_dump($sumB);
function allNull($array) : bool {
foreach($array as $element){
if($element != 0){
return false;
}
}
return true;
}
?>