-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.d
executable file
·47 lines (40 loc) · 1.04 KB
/
main.d
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
#!/usr/bin/rdmd
void main()
{
import std.stdio;
"Part 1: %s".writefln(solve(403, 71920));
"Part 2: %s".writefln(solve(403, 71920*100));
}
ulong solve(int players, int marbles)
{
import std.container.dlist, std.algorithm;
auto board = new DList!ulong();
board.insertBack(0);
ulong[] scores = new ulong[players];
auto rotate = delegate void(int n) {
if(n > 0) {
for(int i = 0; i < n; ++i) {
board.insertBack(board.front());
board.removeFront();
}
} else {
for(int i = 0; i < -n; ++i) {
board.insertFront(board.back());
board.removeBack();
}
}
};
for(int i = 1; i <= marbles; i++) {
if(i % 23) {
rotate(1);
board.insertFront(i);
rotate(1);
} else {
rotate(-8);
scores[i % players] += i + board.front();
board.removeFront();
rotate(1);
}
}
return scores.maxElement;
}