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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
--- Day 22: Slam Shuffle ---
There isn't much to do while you wait for the droids to repair your ship. At least you're drifting
in the right direction. You decide to practice a new card shuffle you've been working on.
Digging through the ship's storage, you find a deck of [1m[97mspace cards[0m! Just like any deck of space
cards, there are 10007 cards in the deck numbered 0 through 10006. The deck must be new - they're
still in [1m[97mfactory order[0m, with 0 on the top, then 1, then 2, and so on, all the way through to 10006
on the bottom.
You've been practicing three different [1m[97mtechniques[0m that you use while shuffling. Suppose you have a
deck of only 10 cards (numbered 0 through 9):
[1m[97mTo deal into new stack[0m, create a new stack of cards by dealing the top card of the deck onto the top
of the new stack repeatedly until you run out of cards:
Top Bottom
0 1 2 3 4 5 6 7 8 9 Your deck
New stack
1 2 3 4 5 6 7 8 9 Your deck
0 New stack
2 3 4 5 6 7 8 9 Your deck
1 0 New stack
3 4 5 6 7 8 9 Your deck
2 1 0 New stack
Several steps later...
9 Your deck
8 7 6 5 4 3 2 1 0 New stack
Your deck
9 8 7 6 5 4 3 2 1 0 New stack
Finally, pick up the new stack you've just created and use it as the deck for the next technique.
[1m[97mTo cut N cards[0m, take the top N cards off the top of the deck and move them as a single unit to the
bottom of the deck, retaining their order. For example, to cut 3:
Top Bottom
0 1 2 3 4 5 6 7 8 9 Your deck
3 4 5 6 7 8 9 Your deck
0 1 2 Cut cards
3 4 5 6 7 8 9 Your deck
0 1 2 Cut cards
3 4 5 6 7 8 9 0 1 2 Your deck
You've also been getting pretty good at a version of this technique where N is negative! In that
case, cut (the absolute value of) N cards from the bottom of the deck onto the top. For example, to
cut -4:
Top Bottom
0 1 2 3 4 5 6 7 8 9 Your deck
0 1 2 3 4 5 Your deck
6 7 8 9 Cut cards
0 1 2 3 4 5 Your deck
6 7 8 9 Cut cards
6 7 8 9 0 1 2 3 4 5 Your deck
[1m[97mTo deal with increment N[0m, start by clearing enough space on your table to lay out all of the cards
individually in a long line. Deal the top card into the leftmost position. Then, move N positions
to the right and deal the next card there. If you would move into a position past the end of the
space on your table, wrap around and keep counting from the leftmost card again. Continue this
process until you run out of cards.
For example, to deal with increment 3:
0 1 2 3 4 5 6 7 8 9 Your deck
. . . . . . . . . . Space on table
^ Current position
Deal the top card to the current position:
1 2 3 4 5 6 7 8 9 Your deck
0 . . . . . . . . . Space on table
^ Current position
Move the current position right 3:
1 2 3 4 5 6 7 8 9 Your deck
0 . . . . . . . . . Space on table
^ Current position
Deal the top card:
2 3 4 5 6 7 8 9 Your deck
0 . . 1 . . . . . . Space on table
^ Current position
Move right 3 and deal:
3 4 5 6 7 8 9 Your deck
0 . . 1 . . 2 . . . Space on table
^ Current position
Move right 3 and deal:
4 5 6 7 8 9 Your deck
0 . . 1 . . 2 . . 3 Space on table
^ Current position
Move right 3, wrapping around, and deal:
5 6 7 8 9 Your deck
0 . 4 1 . . 2 . . 3 Space on table
^ Current position
And so on:
0 7 4 1 8 5 2 9 6 3 Space on table
Positions on the table which already contain cards are still counted; they're not skipped. Of
course, this technique is carefully designed so it will never put two cards in the same position or
leave a position empty.
Finally, collect the cards on the table so that the leftmost card ends up at the top of your deck,
the card to its right ends up just below the top card, and so on, until the rightmost card ends up
at the bottom of the deck.
The complete shuffle process (your puzzle input) consists of applying many of these techniques.
Here are some examples that combine techniques; they all start with a [1m[97mfactory order[0m deck of 10
cards:
deal with increment 7
deal into new stack
deal into new stack
Result: 0 3 6 9 2 5 8 1 4 7
cut 6
deal with increment 7
deal into new stack
Result: 3 0 7 4 1 8 5 2 9 6
deal with increment 7
deal with increment 9
cut -2
Result: 6 3 0 7 4 1 8 5 2 9
deal into new stack
cut -2
deal with increment 7
cut 8
cut -4
deal with increment 7
cut 3
deal with increment 9
deal with increment 3
cut -1
Result: 9 2 5 8 1 4 7 0 3 6
Positions within the deck count from 0 at the top, then 1 for the card immediately below the top
card, and so on to the bottom. (That is, cards start in the position matching their number.)
After shuffling your [1m[97mfactory order[0m deck of 10007 cards, [1m[97mwhat is the position of card 2019?[0m
|