-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday9b.php
156 lines (131 loc) · 3.87 KB
/
day9b.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
print "Part 1: " . part1() . "\n";
print "Part 2: " . part2() . "\n";
function part1(): int
{
return playGame(486, 70833);
}
function part2(): int
{
return playGame(486, 7083300);
}
function playGame(int $elves, int $lastMarble): int
{
$scores = [];
for ($elf = 0; $elf < $elves; $elf++) {
$scores[$elf] = 0;
}
// Initialise the board with elf 0 playing marble 0
$currentMarble = new Marble(0);
$currentMarble->nextMarble = $currentMarble;
$currentMarble->previousMarble = $currentMarble;
$currentElf = 1;
// Just for debugging...
$firstMarble = $currentMarble;
// Play game with the rest of the marbles
for ($marbleScore = 1; $marbleScore <= $lastMarble; $marbleScore++) {
if ($marbleScore % 23 === 0) {
$result = doMove2($marbleScore, $currentMarble);
$scores[$currentElf] += $result['score'];
$currentMarble = $result['currentMarble'];
} else {
$currentMarble = doMove1($marbleScore, $currentMarble);
}
$currentElf = getNextElf($currentElf, $elves);
// debugging:
// showBoard($firstMarble);
if ($marbleScore % 10000 === 0) {
$mem = round(memory_get_usage() / (1024 * 1024), 1);
print "Done $marbleScore marbles - $mem MB used\n";
}
}
return max($scores);
}
function getNextElf(int $currentElf, int $numberOfElves): int
{
$currentElf++;
if ($currentElf === $numberOfElves) {
$currentElf = 0;
}
return $currentElf;
}
/**
* Adds a new marble with the specified score; returns the new marble
* @param int $marbleScore
* @param Marble $currentMarble
* @return Marble
*/
function doMove1(int $marbleScore, Marble $currentMarble): Marble
{
$targetMarble = getMarbleOffset($currentMarble, 1);
$next = $targetMarble->nextMarble;
$newMarble = new Marble($marbleScore);
// Slot in the new marble after the target marble
$newMarble->previousMarble = $targetMarble;
$newMarble->nextMarble = $next;
$targetMarble->nextMarble = $newMarble;
$next->previousMarble = $newMarble;
return $newMarble;
}
/**
* Remove the marble 7 left of the current marble
* @param int $marbleScore
* @param Marble $currentMarble
* @return array
* score => score
* currentMarble = new current marble
*/
function doMove2(int $marbleScore, Marble $currentMarble): array
{
$targetMarble = getMarbleOffset($currentMarble, -7);
// Remove the target marble from the list
$targetMarble->nextMarble->previousMarble = $targetMarble->previousMarble;
$targetMarble->previousMarble->nextMarble = $targetMarble->nextMarble;
return [
'score' => $targetMarble->score + $marbleScore,
'currentMarble' => $targetMarble->nextMarble,
];
}
/**
* Get the marble a specified offset from the current marble
* @param Marble $currentMarble
* @param int $offset
* @return Marble
*/
function getMarbleOffset(Marble $currentMarble, int $offset): Marble
{
if ($offset > 0) {
for ($i = 0; $i < $offset; $i++) {
$currentMarble = $currentMarble->nextMarble;
}
} else {
for ($i = 0; $i < abs($offset); $i++) {
$currentMarble = $currentMarble->previousMarble;
}
}
return $currentMarble;
}
function showBoard(Marble $firstMarble)
{
$out = '';
$currentMarble = $firstMarble;
while ($currentMarble->nextMarble !== $firstMarble) {
$out .= substr(' ' . $currentMarble->score, -3) . ' ';
$currentMarble = $currentMarble->nextMarble;
}
// Add the last marble
$out .= substr(' ' . $currentMarble->score, -3) . ' ';
print $out . "\n";
}
class Marble
{
public $score;
/** @var Marble */
public $nextMarble;
/** @var Marble */
public $previousMarble;
public function __construct(int $score)
{
$this->score = $score;
}
}