You talk to the Elves while you wait for your navigation system to initialize. To pass the time, they introduce you to their favorite marble game.
The Elves play this game by taking turns arranging the marbles in a circle according to very particular rules. The marbles are numbered starting with 0
and increasing by 1
until every marble has a number.
First, the marble numbered 0
is placed in the circle. At this point, while it contains only a single marble, it is still a circle: the marble is both clockwise from itself and counter-clockwise from itself. This marble is designated the current marble.
Then, each Elf takes a turn placing the lowest-numbered remaining marble into the circle between the marbles that are 1
and 2
marbles clockwise of the current marble. (When the circle is large enough, this means that there is one marble between the marble that was just placed and the current marble.) The marble that was just placed then becomes the current marble.
However, if the marble that is about to be placed has a number which is a multiple of 23
, something entirely different happens. First, the current player keeps the marble they would have placed, adding it to their score. In addition, the marble 7
marbles counter-clockwise from the current marble is removed from the circle and also added to the current player's score. The marble located immediately clockwise of the marble that was removed becomes the new current marble.
For example, suppose there are 9 players. After the marble with value 0
is placed in the middle, each player (shown in square brackets) takes a turn. The result of each of those turns would produce circles of marbles like this, where clockwise is to the right and the resulting current marble is in parentheses:
[-] (0)
[1] 0 (1)
[2] 0 (2) 1
[3] 0 2 1 (3)
[4] 0 (4) 2 1 3
[5] 0 4 2 (5) 1 3
[6] 0 4 2 5 1 (6) 3
[7] 0 4 2 5 1 6 3 (7)
[8] 0 (8) 4 2 5 1 6 3 7
[9] 0 8 4 (9) 2 5 1 6 3 7
[1] 0 8 4 9 2(10) 5 1 6 3 7
[2] 0 8 4 9 2 10 5(11) 1 6 3 7
[3] 0 8 4 9 2 10 5 11 1(12) 6 3 7
[4] 0 8 4 9 2 10 5 11 1 12 6(13) 3 7
[5] 0 8 4 9 2 10 5 11 1 12 6 13 3(14) 7
[6] 0 8 4 9 2 10 5 11 1 12 6 13 3 14 7(15)
[7] 0(16) 8 4 9 2 10 5 11 1 12 6 13 3 14 7 15
[8] 0 16 8(17) 4 9 2 10 5 11 1 12 6 13 3 14 7 15
[9] 0 16 8 17 4(18) 9 2 10 5 11 1 12 6 13 3 14 7 15
[1] 0 16 8 17 4 18 9(19) 2 10 5 11 1 12 6 13 3 14 7 15
[2] 0 16 8 17 4 18 9 19 2(20)10 5 11 1 12 6 13 3 14 7 15
[3] 0 16 8 17 4 18 9 19 2 20 10(21) 5 11 1 12 6 13 3 14 7 15
[4] 0 16 8 17 4 18 9 19 2 20 10 21 5(22)11 1 12 6 13 3 14 7 15
[5] 0 16 8 17 4 18(19) 2 20 10 21 5 22 11 1 12 6 13 3 14 7 15
[6] 0 16 8 17 4 18 19 2(24)20 10 21 5 22 11 1 12 6 13 3 14 7 15
[7] 0 16 8 17 4 18 19 2 24 20(25)10 21 5 22 11 1 12 6 13 3 14 7 15
The goal is to be the player with the highest score after the last marble is used up. Assuming the example above ends after the marble numbered 25
, the winning score is 23+9=
32
(because player 5 kept marble 23
and removed marble 9
, while no other player got any points in this very short example game).
Here are a few more examples:
10
players; last marble is worth1618
points: high score is8317
13
players; last marble is worth7999
points: high score is146373
17
players; last marble is worth1104
points: high score is2764
21
players; last marble is worth6111
points: high score is54718
30
players; last marble is worth5807
points: high score is37305
What is the winning Elf's score?
Your puzzle answer was 439089
.
Amused by the speed of your answer, the Elves are curious:
What would the new winning Elf's score be if the number of the last marble were 100 times larger?
Your puzzle answer was 3668541094
.
This puzzle was a nicely put trap which I and certainly a lot other participants promptly fell into. The problem calls for a circular doubly-linked list, which is a variant of a standard data structure commonly taught in Computer Science courses, but with not enough real-world relevance to be included in common languages' standard libraries. (C++'s std::list
is doubly-linked, but not circular, for example.) For small problem sizes, such a structure can be conveniently emulated using a simple array (i.e. a C-style array, a C++ std::vector
, or a Python list
). For part 1 of the puzzle, this works well enough. Since the input (and hence the problem size) is always the same for both parts, I had no qualms about taking this shortcut.
But then part 2 came and blow everything apart. Item insertion and deletion on arrays are O(n) operations, making the whole puzzle O(n²) when implemented this way. Instead of taking 100 times longer, it takes 10k times longer; too much to be manageable, let alone in a relatively slow language like Python!
So I had no other choice than to re-implement the whole thing with a proper circular doubly-linked list, after a frustrating failed attempt at splitting the large list into multiple smaller sub-list (i.e. a two-level tree). For whatever reason (hey, it was 7 AM and I was tired!) I made the proper implementation in C first and only "backported" it to Python later.
(Fun fact: Juggling around with references in (doubly-)linked lists never ceases to inflict headaches, no matter how often you implemented that already!)
The C version is more than two orders of magnitude faster than the Python version, by the way -- so much so, that part 2 in C is twice as fast as part 1 in Python (with lists)! And that's without optimization. (Optimization didn't buy me anything, as I include the compilation time in the runtime here -- sure, gcc -O4
generates 30% faster code, but it takes twice as long to do so ...)
- Part 1, Python (array): 204 bytes, ~500 ms
- Part 2, Python (CDLL): 276 bytes, ~30 s
- Part 2, C (CDLL): 427 bytes, ~200 ms (including compilation)