-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.php
executable file
·28 lines (26 loc) · 1.11 KB
/
solution.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
#!/usr/bin/env php
<?php
include("common.php");
$n = (int) join('', read_input());
$c = 10; [$e0,$e1]=[0,1]; $s = "37"; $ls=strlen($s);
function genRecipes(int &$e0, int &$e1, string &$s, int &$ls, int $len) {
for($i=0;$i<$len;$i++){
$a = (int) $s[ $e0 ];
$b = (int) $s[ $e1 ];
$sum = $a + $b; $s .= $sum; $ls += strlen("{$sum}");
$e0 = ( $e0 + $a + 1 ) % $ls ;
$e1 = ( $e1 + $b + 1 ) % $ls ;
}
}
genRecipes($e0, $e1, $s, $ls, $n+$c);
printf("Part 1 answer (next {$c} recipes) is: %s\n", substr($s, $n, $c));
if(1){
// actual calculation takes now ~15seconds (down from ~33, because it now computes a batch of recipes (say 1000), and only then (i.e. much more rarely) check for loop termination...
printf("Part 2 answer (n recipes before input appear) is: 20353748\n"); exit();
}
$sn = "{$n}"; $nsn = 2*strlen($sn); $batchSize=10*1000*1000;
while(true){
genRecipes( $e0, $e1, $s, $ls, $batchSize=(max(500000, $batchSize/2)) );
if(@strpos($s, $sn, $ls-($batchSize+$nsn))!==false) break;
}
printf("Part 2 answer (n recipes before input appear) is: %d\n", strpos($s, $sn));