-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday22_code.py
115 lines (100 loc) · 3.15 KB
/
day22_code.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
import re
from itertools import combinations
root = "C:/Users/aiden/Dropbox/git/adventofcode/"
lines = [l.rstrip('\n') for l in open(root + 'day21_input.txt', 'r').readlines()]
flatten = lambda t: [item for sublist in t for item in sublist]
lines = open(root + 'day22_input.txt', 'r').read().split("\n\n")
p1 = lines[0].split(":")[1].strip().split("\n")
p2 = lines[1].split(":")[1].strip().split("\n")
p1 = [int(p) for p in p1]
p2 = [int(p) for p in p2]
while True:
if p1 == [] or p2 == []:
break
p1_c = p1.pop(0)
p2_c = p2.pop(0)
if p1_c > p2_c:
p1.append(p1_c)
p1.append(p2_c)
if p2_c > p1_c:
p2.append(p2_c)
p2.append(p1_c)
ans = 0
for i in range(len(p2)):
ans += (len(p2) - i) * p2[i]
## Part 2
lines = [l.rstrip('\n') for l in open(root + 'day21_input.txt', 'r').readlines()]
flatten = lambda t: [item for sublist in t for item in sublist]
lines = open(root + 'day22_input.txt', 'r').read().split("\n\n")
p1 = lines[0].split(":")[1].strip().split("\n")
p2 = lines[1].split(":")[1].strip().split("\n")
p1 = [int(p) for p in p1]
p2 = [int(p) for p in p2]
rounds = []
def subgame(p1, p2):
# print(f"Playing subround of cards p1 {len(p1)} cards {p1} and\np2 {len(p2)} cards {p2}")
rounds = []
while True:
if p1 == [] or p2 == []:
break
p1_c = p1.pop(0)
p2_c = p2.pop(0)
if [p1_c, p2_c] in rounds or (len(p1) == 0 and len(p2) == 0):
p1.append(p1_c)
p1.append(p2_c)
if len(p1) >=p1_c and len(p2) >= p2_c:
winner = subgame(p1[0:p1_c].copy(), p2[0:p2_c].copy())
if winner == 'p1':
p1.append(p1_c)
p1.append(p2_c)
else:
p2.append(p2_c)
p2.append(p1_c)
else:
if p1_c > p2_c:
p1.append(p1_c)
p1.append(p2_c)
if p2_c > p1_c:
p2.append(p2_c)
p2.append(p1_c)
rounds.append([p1_c, p2_c])
if p1 == []:
return 'p2'
elif p2 == []:
return 'p1'
rounds = []
i = 0
while True:
if p1 == [] or p2 == []:
break
p1_c = p1.pop(0)
p2_c = p2.pop(0)
if [p1_c, p2_c] in rounds or (len(p1) == 0 and len(p2) == 0):
print(f"Round {i}... winner p1 cards {p1}")
p1.append(p1_c)
p1.append(p2_c)
if len(p1) >=p1_c and len(p2) >= p2_c:
winner = subgame(p1[0:p1_c].copy(), p2[0:p2_c].copy())
if winner == 'p1':
print(f"Round {i}... winner p1 cards {p1}")
p1.append(p1_c)
p1.append(p2_c)
else:
print(f"Round {i}... winner p2 cards {p2}")
p2.append(p2_c)
p2.append(p1_c)
else:
if p1_c > p2_c:
print(f"Round {i}... winner p1 cards {p1}")
p1.append(p1_c)
p1.append(p2_c)
if p2_c > p1_c:
print(f"Round {i}... winner p2 cards {p2}")
p2.append(p2_c)
p2.append(p1_c)
i += 1
rounds.append([p1_c, p2_c])
ans = 0
for i in range(len(p2)):
ans += (len(p2) - i) * p2[i]
print(ans)