Finished on 2023-12-07 p2
This commit is contained in:
parent
542db4ef55
commit
5d6ae75477
@ -11,11 +11,15 @@ with open(input_f) as file:
|
|||||||
tmp = line.split()
|
tmp = line.split()
|
||||||
cards[tmp[0]] = int(tmp[1])
|
cards[tmp[0]] = int(tmp[1])
|
||||||
|
|
||||||
def value(card):
|
def value(card,p2):
|
||||||
return 'AKQJT98765432'.index(card)
|
return 'AKQJT98765432'.index(card) if not p2 else 'AKQT98765432J'.index(card)
|
||||||
|
|
||||||
def score(card):
|
def score(card,p2):
|
||||||
tmp = sorted(list(Counter(card).values()),reverse=True)
|
tmp = sorted(list(Counter(card).values()),reverse=True)
|
||||||
|
if p2 and (j := card.count('J')) and j < 5:
|
||||||
|
tmp.remove(j)
|
||||||
|
tmp[0] += j
|
||||||
|
tmp = sorted(tmp,reverse=True)
|
||||||
if tmp == [5]:
|
if tmp == [5]:
|
||||||
return 1
|
return 1
|
||||||
elif tmp == [4,1]:
|
elif tmp == [4,1]:
|
||||||
@ -30,18 +34,24 @@ def score(card):
|
|||||||
return 6
|
return 6
|
||||||
elif tmp == [1,1,1,1,1]:
|
elif tmp == [1,1,1,1,1]:
|
||||||
return 7
|
return 7
|
||||||
|
else:
|
||||||
|
print('ERROR')
|
||||||
|
|
||||||
|
def play(cards,p2):
|
||||||
scores = []
|
scores = []
|
||||||
tmp = []
|
tmp = []
|
||||||
for i in cards:
|
for i in cards:
|
||||||
tmp.append(tuple((score(i),tuple(value(c) for c in i),i)))
|
tmp.append(tuple((score(i,p2),tuple(value(c,p2) for c in i),i)))
|
||||||
|
return sorted(tmp, key=lambda x: (x[0], x[1]),reverse=True)
|
||||||
|
|
||||||
|
tmp = play(cards,False)
|
||||||
tmp = sorted(tmp, key=lambda x: (x[0], x[1]),reverse=True)
|
|
||||||
result = 0
|
result = 0
|
||||||
|
|
||||||
for idx,i in enumerate(tmp):
|
for idx,i in enumerate(tmp):
|
||||||
result += cards[i[2]]*(idx+1)
|
result += cards[i[2]]*(idx+1)
|
||||||
print()
|
print('Part 1: ' + str(result))
|
||||||
print(result)
|
|
||||||
|
tmp = play(cards,True)
|
||||||
|
result = 0
|
||||||
|
for idx,i in enumerate(tmp):
|
||||||
|
result += cards[i[2]]*(idx+1)
|
||||||
|
print('Part 2: ' + str(result))
|
||||||
|
19
2023/day7/tests/case1
Normal file
19
2023/day7/tests/case1
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
2345A 1
|
||||||
|
Q2KJJ 13
|
||||||
|
Q2Q2Q 19
|
||||||
|
T3T3J 17
|
||||||
|
T3Q33 11
|
||||||
|
2345J 3
|
||||||
|
J345A 2
|
||||||
|
32T3K 5
|
||||||
|
T55J5 29
|
||||||
|
KK677 7
|
||||||
|
KTJJT 34
|
||||||
|
QQQJA 31
|
||||||
|
JJJJJ 37
|
||||||
|
JAAAA 43
|
||||||
|
AAAAJ 59
|
||||||
|
AAAAA 61
|
||||||
|
2AAAA 23
|
||||||
|
2JJJJ 53
|
||||||
|
JJJJ2 41
|
23
2023/day7/tests/exp
Normal file
23
2023/day7/tests/exp
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
J2566 131
|
||||||
|
K7KK7 272
|
||||||
|
AA222 222
|
||||||
|
222AA 123
|
||||||
|
44T55 467
|
||||||
|
4K339 546
|
||||||
|
42TT2 174
|
||||||
|
TQTTT 710
|
||||||
|
84766 682
|
||||||
|
K22KK 607
|
||||||
|
77595 922
|
||||||
|
26778 768
|
||||||
|
JJ667 198
|
||||||
|
JJ2JJ 123
|
||||||
|
44Q9A 821
|
||||||
|
T6682 851
|
||||||
|
3A232 890
|
||||||
|
5Q5J4 691
|
||||||
|
79789 305
|
||||||
|
KK666 138
|
||||||
|
TT9TT 742
|
||||||
|
Q4276 489
|
||||||
|
6T48J 921
|
9
2023/day7/tests/exp2
Normal file
9
2023/day7/tests/exp2
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
2J345 213
|
||||||
|
22345 425
|
||||||
|
2JJ34 654
|
||||||
|
22234 234
|
||||||
|
2JJJ3 623
|
||||||
|
22223 658
|
||||||
|
J2JJJ 987
|
||||||
|
2JJJJ 456
|
||||||
|
22222 604
|
Loading…
Reference in New Issue
Block a user