-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc_22b.py
67 lines (56 loc) · 1.44 KB
/
aoc_22b.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
fin = open('input_22.txt')
digits = '0123456789'
p = {1:[],2:[]}
for line in fin:
if line[0] in digits:
p[1].append(int(line.strip()))
if line=='\n':
break
for line in fin:
if line[0] in digits:
p[2].append(int(line.strip()))
fin.close()
print(p[1])
print(p[2])
recdepth = 0
def winner(p):
global recdepth
recdepth += 1
print(recdepth,end=" ")
hashes = set()
while True:
thishash = ('.'.join([str(x) for x in p[1]]),'.'.join([str(x) for x in p[2]]))
if thishash in hashes:
recdepth -= 1
return 1,p
else:
hashes.add(thishash)
c1 = p[1][0]
p[1] = p[1][1:]
c2 = p[2][0]
p[2] = p[2][1:]
if c1 > len(p[1]) or c2 > len(p[2]):
if c1 > c2:
p[1].extend([c1,c2])
elif c2 > c1:
p[2].extend([c2,c1])
else:
newdeck = {1:p[1][:c1],2:p[2][:c2]}
if winner(newdeck)[0] == 1:
p[1].extend([c1,c2])
else:
p[2].extend([c2,c1])
if len(p[1])==0:
recdepth -= 1
return 2,p
if len(p[2])==0:
recdepth -= 1
return 1,p
w, deck = winner(p)
print('\nWinner:',w)
print('Player 1:', deck[1])
print('Player 2:', deck[2])
score = 0
for c,el in enumerate(deck[w]):
score += (len(deck[w])-c)*el
print('Score:',score)