-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.php
More file actions
32 lines (24 loc) · 743 Bytes
/
solution.php
File metadata and controls
32 lines (24 loc) · 743 Bytes
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
<?php
ini_set('memory_limit', '4G');
/** @var string[] $input */
preg_match('/^(\d+) players; last marble is worth (\d+) points$/', trim(fgets(STDIN)), $input);
/** @var int $max_marble */
$max_marble = intval($input[2]);
/** @var int[] $scores */
$scores = array_fill(0, intval($input[1]), 0);
/** @var Ds\Deque $deque */
$deque = new Ds\Deque([0]);
for ($i = 1; $i <= 100 * $max_marble; $i++) {
if ($i % 23 === 0) {
$deque->rotate(-7);
$scores[$i % count($scores)] += $i + $deque->pop();
$deque->rotate(1);
} else {
$deque->rotate(1);
$deque->push($i);
}
if ($i === $max_marble) {
fwrite(STDOUT, max($scores) . PHP_EOL);
}
}
fwrite(STDOUT, max($scores) . PHP_EOL);