Compare commits

...

2 Commits

Author SHA1 Message Date
5d6ae75477 Finished on 2023-12-07 p2 2023-12-08 22:11:03 +01:00
542db4ef55 Better version of 2023-12-07 p1 2023-12-08 20:49:41 +01:00
4 changed files with 108 additions and 0 deletions

57
2023/day7/p2_1.py Normal file
View File

@ -0,0 +1,57 @@
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,p2):
return 'AKQJT98765432'.index(card) if not p2 else 'AKQT98765432J'.index(card)
def score(card,p2):
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]:
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
else:
print('ERROR')
def play(cards,p2):
scores = []
tmp = []
for i in cards:
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)
result = 0
for idx,i in enumerate(tmp):
result += cards[i[2]]*(idx+1)
print('Part 1: ' + str(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
View 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
View 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
View File

@ -0,0 +1,9 @@
2J345 213
22345 425
2JJ34 654
22234 234
2JJJ3 623
22223 658
J2JJJ 987
2JJJJ 456
22222 604