-
Notifications
You must be signed in to change notification settings - Fork 0
/
day22.py
371 lines (296 loc) · 11.5 KB
/
day22.py
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# vi: set shiftwidth=4 tabstop=4 expandtab:
import datetime
import os
import math
import functools
import collections
top_dir = os.path.dirname(os.path.abspath(__file__)) + "/../../"
def get_orders_from_file(file_path=top_dir + "resources/year2019_day22_input.txt"):
with open(file_path) as f:
return [l.strip() for l in f]
# Note: Many functions here are basic implementations
# which are mostly used to provide results that are
# easy to check. These results can then be used to
# test trickier and trickier implementations:
# - implement operations on the whole deck
# - implement operations to track a single card
# - implement operations to track a card in reverse
# - optimise the single-card operations using some
# modular trickery.
# For the final result, one can directly skip to the
# function apply_modular_orders.
# Basic operations on full deck
def new_deck(nb):
return list(range(nb))
def deal_into_new_stack(deck):
return list(reversed(deck))
def cut_n_cards(deck, n):
left, right = deck[:n], deck[n:]
return right + left
def deal_with_increment(deck, n):
l = len(deck)
assert math.gcd(l, n) == 1
deck2 = [0] * l
for i in range(l):
deck2[(n * i) % l] = deck[i]
return deck2
# Basic operations on single card
def single_deal_into_new_stack(nb_cards, position):
return (-position - 1) % nb_cards
def single_cut_n_cards(nb_cards, n, position):
return (position - n) % nb_cards
def single_deal_with_increment(nb_cards, n, position):
return (n * position) % nb_cards
# Basic operation on single cards described as modular operation
def modular_deal_into_new_stack():
return (-1, -1)
def modular_cut_into_n_card(n):
return (1, -n)
def modular_deal_with_increment(n):
return (n, 0)
# Reverse operation on single card
def reverse_deal_into_new_stack(nb_cards, position):
"""Same as single_deal_into_new_stack."""
return (-position - 1) % nb_cards
def reverse_cut_n_cards(nb_cards, n, position):
"""Same as single_cut_n_cards with reversed argument."""
return (position + n) % nb_cards
def reverse_deal_with_increment(nb_cards, n, position):
assert math.gcd(nb_cards, n) == 1
# Card i ends up at position j such that: j = (n*i) % nb_cards
# Finding i from j corresponds to finding the inverse of n modulo nb_cards
return (position * modinv(n, nb_cards)) % nb_cards
def xgcd(a, b):
"""Computes the extended gcd."""
# http://anh.cs.luc.edu/331/notes/xgcd.pdf
prevx, x = 1, 0
prevy, y = 0, 1
while b:
q = a // b
x, prevx = prevx - q * x, x
y, prevy = prevy - q * y, y
a, b = b, a % b
return a, prevx, prevy
@functools.lru_cache()
def modinv(a, m):
"""Computes the Modular multiplicative inverse."""
g, x, _ = xgcd(a, m)
if g != 1:
raise Exception("modular inverse does not exist")
return x % m
# Orders
Operation = collections.namedtuple(
"Operation", ["deck", "single", "reverse", "modular"]
)
orders = {
"deal into new stack": Operation(
deal_into_new_stack,
single_deal_into_new_stack,
reverse_deal_into_new_stack,
modular_deal_into_new_stack,
),
"cut ": Operation(
cut_n_cards, single_cut_n_cards, reverse_cut_n_cards, modular_cut_into_n_card
),
"deal with increment ": Operation(
deal_with_increment,
single_deal_with_increment,
reverse_deal_with_increment,
modular_deal_with_increment,
),
}
def get_order_and_remaining(order):
for prefix, op in orders.items():
if order.startswith(prefix):
return op, order[len(prefix) :]
assert False
# Orders on the whole deck
def apply_order(order, deck):
op, rem = get_order_and_remaining(order)
return op.deck(deck, int(rem)) if rem else op.deck(deck)
def apply_orders(nb, orders, repetition=1):
deck = new_deck(nb)
for _ in range(repetition):
for order in orders:
deck = apply_order(order, deck)
return deck
# Orders on single cards
def apply_single_order(order, nb_cards, position):
op, rem = get_order_and_remaining(order)
return op.single(nb_cards, int(rem), position) \
if rem else \
op.single(nb_cards, position)
def apply_single_orders(orders, nb_cards, position):
for order in orders:
position = apply_single_order(order, nb_cards, position)
return position
# Reverse orders on single cards
def apply_reverse_order(order, nb_cards, position):
op, rem = get_order_and_remaining(order)
return op.reverse(nb_cards, int(rem), position) \
if rem else \
op.reverse(nb_cards, position)
def apply_reverse_orders(orders, nb_cards, position):
for order in reversed(orders):
position = apply_reverse_order(order, nb_cards, position)
return position
# Orders on single cards using modular logic
# All orders can be seen as computing
# pos2 = (pos1 * A1 + b1) % nb_cards
# And they can be combined as such
#
# pos3 = (pos2 * A2 + b2) % nb_cards
# = ((pos1 * A1 + b1) * A2 + b2) % nb_cards
# = (pos1 * A1 * A2 + b1 * A2 + b2) % nb_cards
# ~~~~~~~ ~~~~~~~~~~~~
# ~A~ ~B~
#
# Hence, we can summarize an order but also a list of orders
# as a tuple (a, b):
# - it can be applied very efficiently
# - it can be applied in reverse very efficiently too
# - it can be applied multiple times very efficiently
def get_modular_param(orders, repetition, nb_cards):
a, b = 1, 0
for order in orders:
op, rem = get_order_and_remaining(order)
curr_a, curr_b = op.modular(int(rem)) if rem else op.modular()
a = a * curr_a % nb_cards
b = b * curr_a + curr_b % nb_cards
# All orders are summarised as (a, b)
if repetition == 1:
return a, b
# Repetitions can then be computed with exponentiation
# a, a^2, a^3.. a^n
# b, b*(a+1), b*(a^2 + a + 1), b*(a^n+...+a^3+a^2+a+1) = b*(a^(n+1) - 1)/(a - 1)
sum_a = (pow(a, repetition) - 1) // (a - 1)
a = pow(a, repetition)
return a, sum_a * b
def apply_modular_orders(orders, nb_cards, position, repetition=1):
a, b = get_modular_param(orders, repetition, nb_cards)
return (a * position + b) % nb_cards
def apply_modular_reversed_orders(orders, nb_cards, position, repetition=1):
# We have the property:
# pos2 = (pos1 * a + b) % nb_cards
# And we want to compute pos1 from a, b, pos2 and nb_cards
# (pos2 - b) = pos1 * a % nb_cards
# We must find the inverse of a modulo nb_cards
a, b = get_modular_param(orders, repetition, nb_cards)
return ((position - b) * modinv(a, nb_cards)) % nb_cards
# Tests
def test_minimal_operations():
deck = new_deck(10)
assert deck == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
assert deal_into_new_stack(deck) == [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
assert cut_n_cards(deck, 3) == [3, 4, 5, 6, 7, 8, 9, 0, 1, 2]
assert cut_n_cards(deck, -4) == [6, 7, 8, 9, 0, 1, 2, 3, 4, 5]
assert deal_with_increment(deck, 3) == [0, 7, 4, 1, 8, 5, 2, 9, 6, 3]
assert deck == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
def test_single_card_operations():
for nb_cards in [10, 20]:
deck = deal_into_new_stack(new_deck(nb_cards))
for pos, card in enumerate(deck):
assert single_deal_into_new_stack(nb_cards, card) == pos
assert reverse_deal_into_new_stack(nb_cards, pos) == card
a, b = modular_deal_into_new_stack()
assert (a * card + b) % nb_cards == pos
for cut_arg in [3, -4]:
deck = cut_n_cards(new_deck(nb_cards), cut_arg)
a, b = modular_cut_into_n_card(cut_arg)
for pos, card in enumerate(deck):
assert single_cut_n_cards(nb_cards, cut_arg, card) == pos
assert reverse_cut_n_cards(nb_cards, cut_arg, pos) == card
assert (a * card + b) % nb_cards == pos
for incr_arg in [3]:
deck = deal_with_increment(new_deck(nb_cards), incr_arg)
a, b = modular_deal_with_increment(incr_arg)
for pos, card in enumerate(deck):
assert single_deal_with_increment(nb_cards, incr_arg, card) == pos
assert reverse_deal_with_increment(nb_cards, incr_arg, pos) == card
assert (a * card + b) % nb_cards == pos
def test_single_card_orders(nb_cards, orders):
# Test that single card operation tracks every card properly
result = apply_orders(nb_cards, orders)
for pos, card in enumerate(result):
assert apply_single_orders(orders, nb_cards, card) == pos
assert apply_modular_orders(orders, nb_cards, card) == pos
assert apply_reverse_orders(orders, nb_cards, pos) == card
assert apply_modular_reversed_orders(orders, nb_cards, pos) == card
# Test that repetitions of orders can be tracked
result2 = apply_orders(nb_cards, orders, 2)
for pos2, card2 in enumerate(result2):
pos1 = result.index(card2)
# Test with temporary positions
assert apply_single_orders(orders, nb_cards, pos1) == pos2
assert apply_modular_orders(orders, nb_cards, pos1) == pos2
assert apply_reverse_orders(orders, nb_cards, pos2) == pos1
assert apply_modular_reversed_orders(orders, nb_cards, pos2) == pos1
# Test with a single operation
assert apply_modular_orders(orders, nb_cards, card2, 2) == pos2
assert apply_modular_reversed_orders(orders, nb_cards, pos2, 2) == card2
# More iterations to be sure
for rep in [1, 2, 5, 20]:
result = apply_orders(nb_cards, orders, rep)
for pos, card in enumerate(result):
assert apply_modular_orders(orders, nb_cards, card, rep) == pos
assert apply_modular_reversed_orders(orders, nb_cards, pos, rep) == card
def test_orders():
nb_cards = 10
orders = [
"deal with increment 7",
"deal into new stack",
"deal into new stack",
]
assert apply_orders(nb_cards, orders) == [0, 3, 6, 9, 2, 5, 8, 1, 4, 7]
test_single_card_orders(nb_cards, orders)
orders = [
"cut 6",
"deal with increment 7",
"deal into new stack",
]
assert apply_orders(nb_cards, orders) == [3, 0, 7, 4, 1, 8, 5, 2, 9, 6]
test_single_card_orders(nb_cards, orders)
orders = [
"deal with increment 7",
"deal with increment 9",
"cut -2",
]
assert apply_orders(nb_cards, orders) == [6, 3, 0, 7, 4, 1, 8, 5, 2, 9]
test_single_card_orders(nb_cards, orders)
orders = [
"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",
]
assert apply_orders(nb_cards, orders) == [9, 2, 5, 8, 1, 4, 7, 0, 3, 6]
test_single_card_orders(nb_cards, orders)
def run_tests():
test_minimal_operations()
test_single_card_operations()
test_orders()
def part1(orders):
nb_cards = 10007
card_number = 2019
return apply_modular_orders(orders, nb_cards, card_number)
def part2(orders):
nb_cards = 119315717514047
final_position = 2020
nb_shuffle = 101741582076661
return apply_modular_reversed_orders(orders, nb_cards, final_position, nb_shuffle)
def get_solutions():
orders = get_orders_from_file()
print(part1(orders) == 7860)
# print(part2(orders)) - not fast enough >_<
if __name__ == "__main__":
begin = datetime.datetime.now()
run_tests()
get_solutions()
end = datetime.datetime.now()
print(end - begin)