53
SOLUTION MEGATHREAD-🎄- 2021 Day 21 Solutions -🎄-(self.adventofcode)
submitted 4 years, 1 month ago* (edited 20 minutes after) by daggerdragon to /r/adventofcode (134.8k)
Advent of Code 2021: Adventure Time!
- 2 days left to submit your adventures!
- Full details and...
since 4 years, 1 month ago
3 of 3
Tip Reveddit Real-Time can notify you when your content is removed.
your account history
Tip Check if your account has any removed comments.
view my removed comments you are viewing a single comment's thread.
view all comments


Rust:
Part 1 didn't have much of an opportunity for optimization, even the naive solution is pretty fast.
The main time saver for both Part 1 and Part 2 is to identify how to process all three die rolls per turn into a single step:
1 + 2 + 3 = 6move,4 + 5 + 6 = 15 = 5move,7 + 8 + 9 = 24 = 4moves,0 + 1 + 2 = 3moves. Notice that every succession, the die effective rolled value just keeps deceasing by 1. We can use this pattern to simplified the overall turn process.With those attempt, you can solve both part 1 and 2 in a fairly quick time.
For Part 2, we can do much better.
First observation is that player 1 and player 2 does not interact, as now the die is not determinate, but fully probabilistic. So, it is actually possible to calculate every turn for player 1 and 2 separately.
Second observation is that the game must end in a finite number of turns. This is due to every turn, a player is guaranteed to increase their score. We can also observe a particular case where the player traverse from
2, +9 => 1, +3 => 4, +9 => 2 ...averaging 2 points minimum per turn. Because of this, it means for a21point game, we can expect it to last at most 11 turns.With that, what we can do for Part 2 is to calculate how many win/lost condition for each player separately, and then combine them for the total win/lost
What we do is that at turn
N, we figure out how many path for player 1 to win on that specific turn, and how many time player 2 reached turnN - 1, but not win. We can then multiply those two number to get number of winning path for player 1 on turn N. Similarly, we do with for player 2, but use winning paths of player 2 on turnN, and player 1 not winning paths on turnN(this is due to player 2 take their turn after player 1). We then sum up all possible steps (which we know is finite) and we get the total wins for both players.As to calculate the win/lost path count, we just recursively simulate each step for both players, just like the naive method. The reason this is faster is a lot faster that we cut the recursion steps in half, as we can now compute both player 1 + 2's turns separately. Compared with my naive solution, it is around 3000 times faster
Bench:
Code for Part 1 + 2
[removed] content loading...
Thanks, there is indeed no "0".
I've updated the description to correct for that.
The overall point is still the same though. that the game will end in
N / 2turns