Skip to content

Instantly share code, notes, and snippets.

@hammeiam
Last active December 9, 2018 09:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hammeiam/cc9168c883d811c08a737d04fddb2c1e to your computer and use it in GitHub Desktop.
Save hammeiam/cc9168c883d811c08a737d04fddb2c1e to your computer and use it in GitHub Desktop.
Advent of Code 2018: Day 9
"""
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 worth 1618 points: high score is 8317
13 players; last marble is worth 7999 points: high score is 146373
17 players; last marble is worth 1104 points: high score is 2764
21 players; last marble is worth 6111 points: high score is 54718
30 players; last marble is worth 5807 points: high score is 37305
What is the winning Elf's score?
"""
def take_turn(turn_num=0, state=[], curr_idx=0):
next_score = 0
next_turn_num = turn_num + 1
if turn_num == 0:
return (next_turn_num, [0], 0, next_score)
if turn_num % 23 == 0:
remove_idx = (curr_idx - 6) % len(state)
next_score = turn_num + state[remove_idx]
next_state = state[:remove_idx] + state[remove_idx + 1:]
next_active_idx = (remove_idx + 1) % len(next_state)
else:
next_active_idx = (curr_idx + 2) % len(state)
next_state = state[:next_active_idx+1] + [turn_num] + state[next_active_idx+1:]
return (next_turn_num, next_state, next_active_idx, next_score)
def play_game(n_players=1, final_marble_score=0):
players = {}
# init game state
turn_num, state, curr_idx, turn_score = take_turn()
while True:
player = (turn_num + 1) % n_players
turn_num, state, curr_idx, turn_score = take_turn(turn_num, state, curr_idx)
players[player] = players.setdefault(player, 0) + turn_score
if turn_score == final_marble_score:
print("FINAL SCORE ----- ", players[player])
return players[player]
test_cases = [
[10, 1_618, 8_317],
[13, 7_999, 146_373],
[17, 1_104, 2_764],
[21, 6_111, 54_718],
[30, 5_807, 37_305]
]
for (n_players, final_marble_score, high_score) in test_cases:
assert play_game(n_players=n_players, final_marble_score=final_marble_score) == high_score
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment