day 22
This commit is contained in:
parent
043cfe087f
commit
354c655b19
78
2020/22/22.py
Normal file
78
2020/22/22.py
Normal file
|
@ -0,0 +1,78 @@
|
|||
with open("input_sample.txt") as f:
|
||||
p1, p2 = f.read().split('\n\n')
|
||||
p1 = p1.split('\n')[1:]
|
||||
p2 = p2.split('\n')[1:]
|
||||
p1 = list(map(int, p1))
|
||||
p2 = list(map(int, p2))
|
||||
|
||||
i = 1
|
||||
while True:
|
||||
#print(f"-- Round {i} --")
|
||||
#print("Player 1's deck: ", p1)
|
||||
#print("Player 2's deck: ", p2)
|
||||
c1 = p1.pop(0)
|
||||
c2 = p2.pop(0)
|
||||
#print("Player 1 plays: ",c1)
|
||||
#print("Player 2 plays: ",c2)
|
||||
if c1 > c2:
|
||||
#print("Player 1 wins the round!")
|
||||
p1 += [c1, c2]
|
||||
else:
|
||||
#print("Player 2 wins the round!")
|
||||
p2 += [c2, c1]
|
||||
i += 1
|
||||
#print(p1,p2)
|
||||
if len(p1) == 0:
|
||||
winner = p2
|
||||
break
|
||||
if len(p2) == 0:
|
||||
winner = p1
|
||||
break
|
||||
part1 = 0
|
||||
for i, card in enumerate(reversed(winner)):
|
||||
part1 += (i+1)*card
|
||||
print(part1)
|
||||
|
||||
with open("input.txt") as f:
|
||||
p1, p2 = f.read().split('\n\n')
|
||||
p1 = p1.split('\n')[1:]
|
||||
p2 = p2.split('\n')[1:]
|
||||
p1 = list(map(int, p1))
|
||||
p2 = list(map(int, p2))
|
||||
|
||||
def play(p1, p2):
|
||||
seen = set()
|
||||
while True:
|
||||
# does p1 auto-win?
|
||||
state = (tuple(p1), tuple(p2))
|
||||
if state in seen:
|
||||
return 1, p1
|
||||
seen.add(state)
|
||||
# draw cards
|
||||
c1 = p1.pop(0)
|
||||
c2 = p2.pop(0)
|
||||
# should we recurse?
|
||||
if len(p1) >= c1 and len(p2) >= c2: # play recursive round
|
||||
winner, deck = play(p1[:c1],p2[:c2]) # copy that many cards
|
||||
if winner == 1:
|
||||
p1 += [c1, c2]
|
||||
else: # winner == 2
|
||||
p2 += [c2, c1]
|
||||
else: # play normal round
|
||||
if c1 > c2:
|
||||
p1 += [c1, c2]
|
||||
else: # c2 > c1
|
||||
p2 += [c2, c1]
|
||||
# does anyone win by normal rules?
|
||||
if len(p1) == 0:
|
||||
return 2, p2
|
||||
break
|
||||
if len(p2) == 0:
|
||||
return 1, p1
|
||||
|
||||
i, winner = play(p1, p2)
|
||||
|
||||
part2 = 0
|
||||
for i, card in enumerate(reversed(winner)):
|
||||
part2 += (i+1)*card
|
||||
print(part2)
|
53
2020/22/input.txt
Normal file
53
2020/22/input.txt
Normal file
|
@ -0,0 +1,53 @@
|
|||
Player 1:
|
||||
44
|
||||
47
|
||||
29
|
||||
31
|
||||
10
|
||||
40
|
||||
50
|
||||
27
|
||||
35
|
||||
30
|
||||
38
|
||||
11
|
||||
14
|
||||
9
|
||||
42
|
||||
1
|
||||
26
|
||||
24
|
||||
6
|
||||
13
|
||||
8
|
||||
15
|
||||
21
|
||||
18
|
||||
4
|
||||
|
||||
Player 2:
|
||||
17
|
||||
22
|
||||
28
|
||||
34
|
||||
32
|
||||
23
|
||||
3
|
||||
19
|
||||
36
|
||||
12
|
||||
45
|
||||
37
|
||||
46
|
||||
39
|
||||
49
|
||||
43
|
||||
25
|
||||
33
|
||||
2
|
||||
41
|
||||
48
|
||||
7
|
||||
5
|
||||
16
|
||||
20
|
13
2020/22/input_sample.txt
Normal file
13
2020/22/input_sample.txt
Normal file
|
@ -0,0 +1,13 @@
|
|||
Player 1:
|
||||
9
|
||||
2
|
||||
6
|
||||
3
|
||||
1
|
||||
|
||||
Player 2:
|
||||
5
|
||||
8
|
||||
4
|
||||
7
|
||||
10
|
Loading…
Reference in a new issue