-
Notifications
You must be signed in to change notification settings - Fork 17
/
day22.py
executable file
·127 lines (98 loc) · 1.68 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
#!/usr/bin/env python3
from utils.all import *
advent.setup(2020, 22)
fin = advent.get_input()
TESTPLZ = any(a.lower() == 'test' for a in sys.argv[1:])
ftest = io.StringIO('''\
Player 1:
9
2
6
3
1
Player 2:
5
8
4
7
10
''')
if TESTPLZ:
fin = ftest
# eprint(*fin, sep=''); fin.seek(0, 0)
timer_start()
p1, p2 = fin.read().split('\n\n')
p1 = p1.splitlines()
p2 = p2.splitlines()
orig1 = list(map(int, p1[1:]))
orig2 = list(map(int, p2[1:]))
p1 = deque(orig1)
p2 = deque(orig2)
# i = 1
while p1 and p2:
# eprint(i)
# eprint(p1)
# eprint(p2)
# eprint('---'*10)
c1, c2 = p1.popleft(), p2.popleft()
if c1 > c2:
p1.append(c1)
p1.append(c2)
elif c2 > c1:
p2.append(c2)
p2.append(c1)
else:
assert False, 'wtf'
# i += 1
w = p1 if p1 else p2
w = list(w)
n = 1
ans = 0
for c in w[::-1]:
ans += n * c
n += 1
advent.print_answer(1, ans)
def play(p1, p2, gameno=1):
memory = set()
# rnd = 1
while p1 and p2:
# eprint('game', gameno, 'round', rnd)
# eprint(p1)
# eprint(p2)
# eprint('---'*10)
k = (tuple(p1), tuple(p2))
if k in memory:
return 1, p1
memory.add(k)
c1, c2 = p1.popleft(), p2.popleft()
if c1 <= len(p1) and c2 <= len(p2):
l1, l2 = list(p1), list(p2)
l1, l2 = deque(l1[:c1]), deque(l2[:c2])
winner, _ = play(l1, l2, gameno + 1)
else:
if c1 > c2:
winner = 1
else:
winner = 2
if winner == 1:
p1.append(c1)
p1.append(c2)
else:
p2.append(c2)
p2.append(c1)
# rnd += 1
w = p1 if p1 else p2
w = list(w)
return 1 if p1 else 2, w
p1 = deque(orig1)
p2 = deque(orig2)
p, w = play(p1, p2)
w = list(w)
n = 1
ans2 = 0
for c in w[::-1]:
ans2 += n * c
n += 1
# 32448 wrong
# 33880 wrong
advent.print_answer(2, ans2)