-
Notifications
You must be signed in to change notification settings - Fork 0
/
9.1.php
70 lines (48 loc) · 1.3 KB
/
9.1.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
$input = <<<EOT
418 players; last marble is worth 70769 points
EOT;
preg_match('/(?<players>[0-9]+) players; last marble is worth (?<points>[0-9]+) points/', $input, $matches);
$players = $matches['players'];
$last_score = $matches['points'];
$circle = [0];
$current_index = 0;
$player = 1;
$scores = [];
for ($i = 1; $i <= $players; $i++) {
$scores[$i] = 0;
}
$x = 1;
while ($x <= $last_score) {
if ($x % 23 === 0) {
$scores[$player] += $x;
$current_index -= 7;
if ($current_index < 0) {
$current_index = count($circle) - abs($current_index);
}
$scores[$player] += $circle[$current_index];
array_splice($circle, $current_index, 1);
if ($current_index === count($circle)) {
$current_index = 0;
}
} else {
if ($current_index + 2 <= count($circle)) {
$current_index += 2;
} elseif ($current_index + 1 === count($circle) - 1) {
$current_index = 0;
} elseif ($current_index === count($circle) - 1) {
$current_index = 1;
}
array_splice($circle, $current_index, 0, $x);
}
$player++;
$player = $player > $players ? 1 : $player;
$x++;
}
$highest_score = null;
foreach ($scores as $score) {
if ($score > $highest_score || $highest_score === null) {
$highest_score = $score;
}
}
echo $highest_score;