doing old challenges for the challenge

This commit is contained in:
Aeryn
2024-12-05 10:56:00 +01:00
parent 9d8231777d
commit a2ae92bd91
2 changed files with 108 additions and 0 deletions

55
2020/22/22.py Normal file
View File

@@ -0,0 +1,55 @@
def playRound(p1,p2):
print("-- ROUND RESULT --")
print("Player 1's deck: "+str(p1))
print("Player 2's deck: "+str(p2))
print("Player 1 plays "+str(p1[0]))
print("Player 2 plays "+str(p2[0]))
if p1[0]>p2[0]:
p1.append(p1[0])
p1.pop(0)
p1.append(p2[0])
p2.pop(0)
print("Player 1 wins the round!")
elif p2[0]>p1[0]:
p2.append(p2[0])
p2.pop(0)
p2.append(p1[0])
p1.pop(0)
print("Player 2 wins the round!")
print("\n\n")
return [p1,p2]
def calcScore(deck):
out = 0
for i in range(0,len(deck)):
print(str(i+1)+"th last card in the deck: "+str(deck[-(i+1)]))
print("value: "+str((i+1)*deck[-(i+1)]))
out += (i+1)*deck[-(i+1)]
print("running total: "+str(out))
return out
flag = False
p1 = []
p2 = []
with open("input22.txt") as file:
for line in file:
if line == "Player 1:\n":
pass
elif line == "Player 2:\n":
flag = True
elif line == "\n":
pass
elif not flag:
p1.append(int(line[:-1]))
else:
p2.append(int(line[:-1]))
while len(p1) > 0 and len(p2) > 0:
res = playRound(p1,p2)
p1 = res[0]
p2 = res[1]
print("== Post-game results ==")
print("Player 1's deck: "+str(p1))
print("Player 2's deck: "+str(p2))
out = calcScore(p1) if len(p1) > 0 else calcScore(p2)
print(out)

53
2020/22/input22.txt Normal file
View File

@@ -0,0 +1,53 @@
Player 1:
42
29
12
40
47
26
11
39
41
13
8
50
44
33
5
27
10
25
17
1
28
22
6
32
35
Player 2:
19
34
38
21
43
14
23
46
16
3
36
31
37
45
30
15
49
48
24
9
2
18
4
7
20