-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path9.2.php
More file actions
86 lines (56 loc) · 1.56 KB
/
9.2.php
File metadata and controls
86 lines (56 loc) · 1.56 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?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'] * 100;
$circle = [
0 => [
'clockwise' => 0,
'counterclockwise' => 0,
],
];
$current_index = 0;
$player = 1;
$scores = [];
for ($i = 1; $i <= $players; $i++) {
$scores[$i] = 0;
}
$last_marble = 0;
$x = 1;
while ($x <= $last_score) {
if ($x % 23 === 0) {
$marble_to_remove = $circle[$last_marble]['counterclockwise'];
for ($i = 0; $i < 6; $i++) {
$marble_to_remove = $circle[$marble_to_remove]['counterclockwise'];
}
$before = $circle[$marble_to_remove]['counterclockwise'];
$after = $circle[$marble_to_remove]['clockwise'];
$circle[$before]['clockwise'] = $after;
$circle[$after]['counterclockwise'] = $before;
unset($circle[$marble_to_remove]);
$scores[$player] += $marble_to_remove + $x;
$last_marble = $after;
} else {
$before = $circle[$last_marble]['clockwise'];
$after = $circle[$before]['clockwise'];
$circle[$x] = [
'counterclockwise' => $before,
'clockwise' => $after,
];
$circle[$after]['counterclockwise'] = $x;
$circle[$before]['clockwise'] = $x;
$last_marble = $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;