Files
AdventOfCode/2020/22/22.py
2024-12-05 10:56:00 +01:00

55 lines
1.4 KiB
Python

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)