-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathday23.py
executable file
·61 lines (43 loc) · 1.25 KB
/
day23.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
#!/usr/bin/env python3
import sys
def build_list(cups, n=None):
initial_sz = max(cups) + 1
next_cup = [0] * initial_sz
for prev, cur in zip(cups, cups[1:]):
next_cup[prev] = cur
if n is None:
next_cup[cups[-1]] = cups[0]
else:
next_cup += list(range(initial_sz + 1, n + 2))
next_cup[n] = cups[0]
next_cup[cups[-1]] = initial_sz
return next_cup
def play(cur, next_cup, n_rounds):
max_cup = len(next_cup) - 1
for _ in range(n_rounds):
first = next_cup[cur]
mid = next_cup[first]
last = next_cup[mid]
picked = (first, mid, last)
next_cup[cur] = next_cup[last]
dst = max_cup if cur == 1 else cur - 1
while dst in picked:
dst = max_cup if dst == 1 else dst - 1
next_cup[last] = next_cup[dst]
next_cup[dst] = first
cur = next_cup[cur]
# Open the first argument as input or use stdin if no arguments were given
fin = open(sys.argv[1]) if len(sys.argv) > 1 else sys.stdin
orig = tuple(map(int, fin.readline().rstrip()))
next_cup = build_list(orig)
play(orig[0], next_cup, 100)
ans = ''
cur = next_cup[1]
while cur != 1:
ans += str(cur)
cur = next_cup[cur]
print('Part 1:', ans)
next_cup = build_list(orig, 1000000)
play(orig[0], next_cup, 10000000)
ans = next_cup[1] * next_cup[next_cup[1]]
print('Part 2:', ans)