-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path17.php
45 lines (34 loc) · 760 Bytes
/
17.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
<?php
$input = 394;
$buffer = [0];
$pos = 0;
function calc($cnt, $pos, $offset)
{
$pos += $offset;
return $pos % $cnt;
}
$iter = 1;
do {
$pos = calc(count($buffer), $pos, $input)+1;
$rem = array_splice($buffer, $pos);
$buffer = array_merge($buffer, [$iter], $rem);
$iter++;
} while ($iter <= 2017);
$key = array_search(2017, $buffer);
echo 'Part A: ' . $buffer[$key+1] . PHP_EOL;
// ---------
$input = 394;
$buffer = [ 0, null ];
$pos = 0;
$iter = 1;
do {
$pos = calc($iter, $pos, $input)+1;
if ($pos == 1) {
$buffer[$pos] = $iter;
}
$iter++;
if ($iter % 10000 == 0) {
echo ' > ' . number_format($iter) . PHP_EOL;
}
} while ($iter <= 50000000);
echo 'Part B: ' . $buffer[1] . PHP_EOL;