Skip to content

Commit b197e99

Browse files
committed
Advent of Code (Python): solve 2019/22 p1
1 parent fe4d185 commit b197e99

1 file changed

Lines changed: 66 additions & 0 deletions

File tree

advent_of_code/2019/d22.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/bin/python
2+
"""Advent of Code, Day 22."""
3+
# https://www.reddit.com/r/adventofcode/comments/ee0rqi/comment/fbwp0r0/
4+
5+
import collections
6+
PARSER = str.splitlines
7+
8+
9+
def solve(data: list[str], part: int, testing: bool) -> int:
10+
"""Return a card after shuffling."""
11+
want_card = 0 if testing else 2019 if part == 1 else 2020
12+
deck_size = 10 if testing else 10007 if part == 1 else 119315717514047
13+
# cards = collections.deque(range(deck_size))
14+
# position = list(cards).index(want_card)
15+
position = want_card
16+
for _ in range(1 if part == 1 else 101741582076661):
17+
for cmd in data:
18+
if cmd == "deal into new stack":
19+
# cards.reverse()
20+
position = deck_size - 1 - position
21+
elif cmd.startswith("cut"):
22+
n = int(cmd.split()[-1])
23+
# cards.rotate(-n)
24+
position = (position - n) % deck_size
25+
elif cmd.startswith("deal with increment"):
26+
n = int(cmd.split()[-1])
27+
# new_cards = [0] * deck_size
28+
# for i, card in enumerate(cards):
29+
# new_cards[(i * n) % deck_size] = card
30+
# cards = collections.deque(new_cards)
31+
position = (position * n) % deck_size
32+
33+
return position
34+
35+
36+
SAMPLE = [
37+
"""\
38+
deal with increment 7
39+
deal into new stack
40+
deal into new stack""",
41+
"""\
42+
cut 6
43+
deal with increment 7
44+
deal into new stack""",
45+
"""\
46+
deal with increment 7
47+
deal with increment 9
48+
cut -2""",
49+
"""\
50+
deal into new stack
51+
cut -2
52+
deal with increment 7
53+
cut 8
54+
cut -4
55+
deal with increment 7
56+
cut 3
57+
deal with increment 9
58+
deal with increment 3
59+
cut -1""",
60+
]
61+
TESTS = [
62+
(1, SAMPLE[0], 0),
63+
(1, SAMPLE[1], 3),
64+
(1, SAMPLE[2], 6),
65+
(1, SAMPLE[3], 9),
66+
]

0 commit comments

Comments
 (0)