Skip to content

Commit 4e43e56

Browse files
committed
Day 22 2019 Part 1
1 parent 8d0ca58 commit 4e43e56

File tree

3 files changed

+288
-0
lines changed

3 files changed

+288
-0
lines changed

2019/day-22/README.txt

+159
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
--- Day 22: Slam Shuffle ---
2+
3+
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.
4+
5+
Digging through the ship's storage, you find a deck of space cards! 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 factory order, with 0 on the top, then 1, then 2, and so on, all the way through to 10006 on the bottom.
6+
7+
You've been practicing three different techniques that you use while shuffling. Suppose you have a deck of only 10 cards (numbered 0 through 9):
8+
9+
To deal into new stack, 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:
10+
11+
Top Bottom
12+
0 1 2 3 4 5 6 7 8 9 Your deck
13+
New stack
14+
15+
1 2 3 4 5 6 7 8 9 Your deck
16+
0 New stack
17+
18+
2 3 4 5 6 7 8 9 Your deck
19+
1 0 New stack
20+
21+
3 4 5 6 7 8 9 Your deck
22+
2 1 0 New stack
23+
24+
Several steps later...
25+
26+
9 Your deck
27+
8 7 6 5 4 3 2 1 0 New stack
28+
29+
Your deck
30+
9 8 7 6 5 4 3 2 1 0 New stack
31+
32+
Finally, pick up the new stack you've just created and use it as the deck for the next technique.
33+
34+
To cut N cards, 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:
35+
36+
Top Bottom
37+
0 1 2 3 4 5 6 7 8 9 Your deck
38+
39+
3 4 5 6 7 8 9 Your deck
40+
0 1 2 Cut cards
41+
42+
3 4 5 6 7 8 9 Your deck
43+
0 1 2 Cut cards
44+
45+
3 4 5 6 7 8 9 0 1 2 Your deck
46+
47+
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:
48+
49+
Top Bottom
50+
0 1 2 3 4 5 6 7 8 9 Your deck
51+
52+
0 1 2 3 4 5 Your deck
53+
6 7 8 9 Cut cards
54+
55+
0 1 2 3 4 5 Your deck
56+
6 7 8 9 Cut cards
57+
58+
6 7 8 9 0 1 2 3 4 5 Your deck
59+
60+
To deal with increment N, 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.
61+
62+
For example, to deal with increment 3:
63+
64+
65+
0 1 2 3 4 5 6 7 8 9 Your deck
66+
. . . . . . . . . . Space on table
67+
^ Current position
68+
69+
Deal the top card to the current position:
70+
71+
1 2 3 4 5 6 7 8 9 Your deck
72+
0 . . . . . . . . . Space on table
73+
^ Current position
74+
75+
Move the current position right 3:
76+
77+
1 2 3 4 5 6 7 8 9 Your deck
78+
0 . . . . . . . . . Space on table
79+
^ Current position
80+
81+
Deal the top card:
82+
83+
2 3 4 5 6 7 8 9 Your deck
84+
0 . . 1 . . . . . . Space on table
85+
^ Current position
86+
87+
Move right 3 and deal:
88+
89+
3 4 5 6 7 8 9 Your deck
90+
0 . . 1 . . 2 . . . Space on table
91+
^ Current position
92+
93+
Move right 3 and deal:
94+
95+
4 5 6 7 8 9 Your deck
96+
0 . . 1 . . 2 . . 3 Space on table
97+
^ Current position
98+
99+
Move right 3, wrapping around, and deal:
100+
101+
5 6 7 8 9 Your deck
102+
0 . 4 1 . . 2 . . 3 Space on table
103+
^ Current position
104+
105+
And so on:
106+
107+
0 7 4 1 8 5 2 9 6 3 Space on table
108+
109+
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.
110+
111+
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.
112+
113+
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 factory order deck of 10 cards:
114+
115+
deal with increment 7
116+
deal into new stack
117+
deal into new stack
118+
Result: 0 3 6 9 2 5 8 1 4 7
119+
120+
cut 6
121+
deal with increment 7
122+
deal into new stack
123+
Result: 3 0 7 4 1 8 5 2 9 6
124+
125+
deal with increment 7
126+
deal with increment 9
127+
cut -2
128+
Result: 6 3 0 7 4 1 8 5 2 9
129+
130+
deal into new stack
131+
cut -2
132+
deal with increment 7
133+
cut 8
134+
cut -4
135+
deal with increment 7
136+
cut 3
137+
deal with increment 9
138+
deal with increment 3
139+
cut -1
140+
Result: 9 2 5 8 1 4 7 0 3 6
141+
142+
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.)
143+
144+
After shuffling your factory order deck of 10007 cards, what is the position of card 2019?
145+
146+
--- Part Two ---
147+
148+
After a while, you realize your shuffling skill won't improve much more with merely a single deck of cards. You ask every 3D printer on the ship to make you some more cards while you check on the ship repairs. While reviewing the work the droids have finished so far, you think you see Halley's Comet fly past!
149+
150+
When you get back, you discover that the 3D printers have combined their power to create for you a single, giant, brand new, factory order deck of 119315717514047 space cards.
151+
152+
Finally, a deck of cards worthy of shuffling!
153+
154+
You decide to apply your complete shuffle process (your puzzle input) to the deck 101741582076661 times in a row.
155+
156+
You'll need to be careful, though - one wrong move with this many cards and you might overflow your entire ship!
157+
158+
After shuffling your new, giant, factory order deck that many times, what number is on the card that ends up in position 2020?
159+

2019/day-22/day-22-input.txt

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
deal with increment 65
2+
deal into new stack
3+
deal with increment 25
4+
cut -6735
5+
deal with increment 3
6+
cut 8032
7+
deal with increment 71
8+
cut -4990
9+
deal with increment 66
10+
deal into new stack
11+
cut -8040
12+
deal into new stack
13+
deal with increment 18
14+
cut -8746
15+
deal with increment 42
16+
deal into new stack
17+
deal with increment 17
18+
cut -8840
19+
deal with increment 55
20+
cut -4613
21+
deal with increment 10
22+
cut -5301
23+
deal into new stack
24+
deal with increment 21
25+
cut -5653
26+
deal with increment 2
27+
cut 5364
28+
deal with increment 72
29+
cut -3468
30+
deal into new stack
31+
cut -9661
32+
deal with increment 63
33+
cut 6794
34+
deal with increment 43
35+
cut 2935
36+
deal with increment 66
37+
cut -1700
38+
deal with increment 6
39+
cut 5642
40+
deal with increment 64
41+
deal into new stack
42+
cut -5699
43+
deal with increment 43
44+
cut -9366
45+
deal with increment 42
46+
deal into new stack
47+
cut 2364
48+
deal with increment 13
49+
cut 8080
50+
deal with increment 2
51+
deal into new stack
52+
cut -9602
53+
deal with increment 51
54+
cut 3214
55+
deal into new stack
56+
cut -2995
57+
deal with increment 57
58+
cut -8169
59+
deal into new stack
60+
cut 362
61+
deal with increment 41
62+
cut -4547
63+
deal with increment 56
64+
cut -1815
65+
deal into new stack
66+
cut 1554
67+
deal with increment 71
68+
cut 2884
69+
deal with increment 44
70+
cut -2423
71+
deal with increment 4
72+
deal into new stack
73+
deal with increment 20
74+
cut -2242
75+
deal with increment 48
76+
cut -716
77+
deal with increment 29
78+
cut -6751
79+
deal with increment 45
80+
cut -9511
81+
deal with increment 75
82+
cut -440
83+
deal with increment 35
84+
cut 6861
85+
deal with increment 52
86+
cut -4702
87+
deal into new stack
88+
deal with increment 28
89+
cut 305
90+
deal with increment 16
91+
cut 7094
92+
deal into new stack
93+
cut 5175
94+
deal with increment 30
95+
deal into new stack
96+
deal with increment 61
97+
cut 1831
98+
deal into new stack
99+
deal with increment 25
100+
cut 4043

2019/day-22/day-22-part-1.rb

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env ruby
2+
3+
file_path = File.expand_path("../day-22-input.txt", __FILE__)
4+
input = File.read(file_path)
5+
6+
deck_size = 10007
7+
8+
deck = (0..deck_size-1).to_a
9+
10+
input.each_line do |line|
11+
if line.match(/deal into new stack/)
12+
deck.reverse!
13+
elsif line.match(/cut (-*\d+)/)
14+
cut = $1.to_i
15+
deck.rotate!(cut)
16+
elsif line.match(/deal with increment (\d+)/)
17+
incr = $1.to_i
18+
19+
deck_copy = Array.new(deck_size)
20+
21+
deck_size.times do |card|
22+
deck_copy[(card * incr) % deck_size] = deck[card]
23+
end
24+
25+
deck = deck_copy
26+
end
27+
end
28+
29+
puts deck.index(2019)

0 commit comments

Comments
 (0)