Better version of 2023-12-07 p1
This commit is contained in:
parent
7c9ef891ae
commit
542db4ef55
47
2023/day7/p2_1.py
Normal file
47
2023/day7/p2_1.py
Normal file
@ -0,0 +1,47 @@
|
||||
import sys
|
||||
from pprint import pp
|
||||
from collections import Counter
|
||||
|
||||
input_f = sys.argv[1]
|
||||
|
||||
cards = {}
|
||||
|
||||
with open(input_f) as file:
|
||||
for line in file:
|
||||
tmp = line.split()
|
||||
cards[tmp[0]] = int(tmp[1])
|
||||
|
||||
def value(card):
|
||||
return 'AKQJT98765432'.index(card)
|
||||
|
||||
def score(card):
|
||||
tmp = sorted(list(Counter(card).values()),reverse=True)
|
||||
if tmp == [5]:
|
||||
return 1
|
||||
elif tmp == [4,1]:
|
||||
return 2
|
||||
elif tmp == [3,2]:
|
||||
return 3
|
||||
elif tmp == [3,1,1]:
|
||||
return 4
|
||||
elif tmp == [2,2,1]:
|
||||
return 5
|
||||
elif tmp == [2,1,1,1]:
|
||||
return 6
|
||||
elif tmp == [1,1,1,1,1]:
|
||||
return 7
|
||||
|
||||
|
||||
scores = []
|
||||
tmp = []
|
||||
for i in cards:
|
||||
tmp.append(tuple((score(i),tuple(value(c) for c in i),i)))
|
||||
|
||||
|
||||
tmp = sorted(tmp, key=lambda x: (x[0], x[1]),reverse=True)
|
||||
result = 0
|
||||
|
||||
for idx,i in enumerate(tmp):
|
||||
result += cards[i[2]]*(idx+1)
|
||||
print()
|
||||
print(result)
|
Loading…
Reference in New Issue
Block a user