diff --git a/21/input.txt b/21/input.txt new file mode 100644 index 0000000..2de620f --- /dev/null +++ b/21/input.txt @@ -0,0 +1,2 @@ +Player 1 starting position: 7 +Player 2 starting position: 8 \ No newline at end of file diff --git a/21/problem.html b/21/problem.html new file mode 100644 index 0000000..97d18a9 --- /dev/null +++ b/21/problem.html @@ -0,0 +1,54 @@ + + + + + + + Day 21 - Advent of Code 2021 + +

--- Day 21: Dirac Dice ---

There's not much to do as you slowly descend to the bottom of the ocean. The submarine computer challenges you to a nice game of Dirac Dice.

+

This game consists of a single die, two pawns, and a game board with a circular track containing ten spaces marked 1 through 10 clockwise. Each player's starting space is chosen randomly (your puzzle input). Player 1 goes first.

+

Players take turns moving. On each player's turn, the player rolls the die three times and adds up the results. Then, the player moves their pawn that many times forward around the track (that is, moving clockwise on spaces in order of increasing value, wrapping back around to 1 after 10). So, if a player is on space 7 and they roll 2, 2, and 1, they would move forward 5 times, to spaces 8, 9, 10, 1, and finally stopping on 2.

+

After each player moves, they increase their score by the value of the space their pawn stopped on. Players' scores start at 0. So, if the first player starts on space 7 and rolls a total of 5, they would stop on space 2 and add 2 to their score (for a total score of 2). The game immediately ends as a win for any player whose score reaches at least 1000.

+

Since the first game is a practice game, the submarine opens a compartment labeled deterministic dice and a 100-sided die falls out. This die always rolls 1 first, then 2, then 3, and so on up to 100, after which it starts over at 1 again. Play using this die.

+

For example, given these starting positions:

+
Player 1 starting position: 4
+Player 2 starting position: 8
+
+

This is how the game would go:

+ +

...after many turns...

+ +

Since player 1 has at least 1000 points, player 1 wins and the game ends. At this point, the losing player had 745 points and the die had been rolled a total of 993 times; 745 * 993 = 739785.

+

Play a practice game using the deterministic 100-sided die. The moment either player wins, what do you get if you multiply the score of the losing player by the number of times the die was rolled during the game?

+

Your puzzle answer was 556206.

--- Part Two ---

Now that you're warmed up, it's time to play the real game.

+

A second compartment opens, this time labeled Dirac dice. Out of it falls a single three-sided die.

+

As you experiment with the die, you feel a little strange. An informational brochure in the compartment explains that this is a quantum die: when you roll it, the universe splits into multiple copies, one copy for each possible outcome of the die. In this case, rolling the die always splits the universe into three copies: one where the outcome of the roll was 1, one where it was 2, and one where it was 3.

+

The game is played the same as before, although to prevent things from getting too far out of hand, the game now ends when either player's score reaches at least 21.

+

Using the same starting positions as in the example above, player 1 wins in 444356092776315 universes, while player 2 merely wins in 341960390180808 universes.

+

Using your given starting positions, determine every possible outcome. Find the player that wins in more universes; in how many universes does that player win?

+

Your puzzle answer was 630797200227453.

\ No newline at end of file diff --git a/21/solution.c b/21/solution.c new file mode 100644 index 0000000..bc5720f --- /dev/null +++ b/21/solution.c @@ -0,0 +1,29 @@ +#include + + + M( p,s){ return(p + +s-1)% 10+1;}S( int*p,int*d, + int* D){for(int s= 0;s<3;s++,( + *D) ++)(*p)=M(* p,* d=*d%100+1 + );return*p;}x(l,r ){int L=0,R=0 + ,d=0,D=0;for(;; ){L+=S( + &l,&d,&D); if(L>= + 1000) break;R + +=S(&r, &d,&D); + if( R >= 1000)break + ;}return D* (L>R?R:L);} + size_t X(t, l,r,L,R){if(L >=21 + ||R >= 21 ) return t;static F[ ]={1,3,6 + ,7 , 6,3,1}; size_t s=0;for( int m=0;m<7; + m++ ){int o=l, O= L,g=r,G=R + ;if (t)G+=g=M(r ,m +3);else + O+=o=M(l,m+3);s+=F[ m] *X(! t,o,g,O + ,G);}return s;}main ( int p, char* * P) + { int l =4,r=8 ;if( p ==3 + ){ l= atoi(P[ 1 ]); + r=atoi (P[2 ] ); + }printf("" "" "Si" + "lver: %" "" "d" + "\nGold: " "" "%lu\n" + ,x(l,r),X ( 0,l,r, + 0,0));}