From 5d6ae754779b84340da8f4b679abbe2c556c5668 Mon Sep 17 00:00:00 2001 From: FrederikBaerentsen Date: Fri, 8 Dec 2023 22:11:03 +0100 Subject: [PATCH] Finished on 2023-12-07 p2 --- 2023/day7/p2_1.py | 36 +++++++++++++++++++++++------------- 2023/day7/tests/case1 | 19 +++++++++++++++++++ 2023/day7/tests/exp | 23 +++++++++++++++++++++++ 2023/day7/tests/exp2 | 9 +++++++++ 4 files changed, 74 insertions(+), 13 deletions(-) create mode 100644 2023/day7/tests/case1 create mode 100644 2023/day7/tests/exp create mode 100644 2023/day7/tests/exp2 diff --git a/2023/day7/p2_1.py b/2023/day7/p2_1.py index 9195981..8113a9d 100644 --- a/2023/day7/p2_1.py +++ b/2023/day7/p2_1.py @@ -11,11 +11,15 @@ with open(input_f) as file: tmp = line.split() cards[tmp[0]] = int(tmp[1]) -def value(card): - return 'AKQJT98765432'.index(card) +def value(card,p2): + 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) + 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]: @@ -30,18 +34,24 @@ def score(card): 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) -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) +tmp = play(cards,False) result = 0 - for idx,i in enumerate(tmp): result += cards[i[2]]*(idx+1) -print() -print(result) +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)) diff --git a/2023/day7/tests/case1 b/2023/day7/tests/case1 new file mode 100644 index 0000000..dcc876c --- /dev/null +++ b/2023/day7/tests/case1 @@ -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 diff --git a/2023/day7/tests/exp b/2023/day7/tests/exp new file mode 100644 index 0000000..c0e0da8 --- /dev/null +++ b/2023/day7/tests/exp @@ -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 diff --git a/2023/day7/tests/exp2 b/2023/day7/tests/exp2 new file mode 100644 index 0000000..fc14b25 --- /dev/null +++ b/2023/day7/tests/exp2 @@ -0,0 +1,9 @@ +2J345 213 +22345 425 +2JJ34 654 +22234 234 +2JJJ3 623 +22223 658 +J2JJJ 987 +2JJJJ 456 +22222 604