-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.php
executable file
·43 lines (43 loc) · 1.69 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env php
<?php
include("common.php");
system(sprintf("cat %s/input | %s/solution ", SDIR, SDIR)); exit(0); // call the C one, since the PHP variant without a deque is too slow.
/// The following is the code to the old array_splice based solution (too slow for part 2):
$lines = read_input();
$nPlayers=0; $nMarbles=0;
foreach($lines as $line) {
[$nPlayers, $nMarbles] = line2digits($line);
printf("Part 1 ");slow_game("Part 1", $nPlayers, $nMarbles);
printf("Part 2 ");slow_game("Part 2", $nPlayers, $nMarbles*100);
}
function slow_game(string $label, int $nPlayers, int $nMarbles): int {
if($nMarbles>9000){
printf("!!!!!!! marbles: %d are over 9000. Try instead 'solution.deque.php' or the C based 'solution' .\n",$nMarbles);
return 0;
}
$placed = [0];
$players = Azeros($nPlayers+1);
$marbles = range(1, $nMarbles);
$c = 0; $p = 1; $placedLength = count($placed);
foreach ($marbles as $m) {
if(0 === ($m % 10000))printf("Placing marble %d.\n", $m);
if (0 === ($m % 23)) {
$removedIndex = (($placedLength + $c - 7) % $placedLength) + 1;
$removed = $placed[$removedIndex];
array_splice($placed, $removedIndex, 1);
$players[$p] += ($m + $removed);
$c = $removedIndex - 1;
$placedLength--;
} else {
$newC = ($c + 2) % $placedLength;
array_splice($placed, $newC + 1, 0, $m);
$c = $newC;
$placedLength++;
}
$p++;
if ($p > $nPlayers) $p = 1;
}
$pv = Amax($players);
printf("%s Highscore (for %5d players and %8d marbles) is: %18d \n", $label, $nPlayers, $nMarbles, $pv);
return $pv;
}