From 27f66da8f869317417f795d955e6fdc3aaddd823 Mon Sep 17 00:00:00 2001 From: FrederikBaerentsen Date: Thu, 7 Dec 2023 21:41:09 +0100 Subject: [PATCH] Working on 2023-12-07 part2. Missing tiebreaker --- 2023/day7/exp | 34 +++++--- 2023/day7/exp2 | 9 ++ 2023/day7/part2.py | 201 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 233 insertions(+), 11 deletions(-) create mode 100644 2023/day7/exp2 create mode 100644 2023/day7/part2.py diff --git a/2023/day7/exp b/2023/day7/exp index 1ab7a06..c0e0da8 100644 --- a/2023/day7/exp +++ b/2023/day7/exp @@ -1,11 +1,23 @@ -32T3K 765 -T55J5 684 -AA8AA 21 -KK677 28 -AAAAA 123 -KTJJT 220 -23332 1 -A23A4 64 -QQQJA 483 -23456 64 -HNJIP 317 +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/exp2 b/2023/day7/exp2 new file mode 100644 index 0000000..fc14b25 --- /dev/null +++ b/2023/day7/exp2 @@ -0,0 +1,9 @@ +2J345 213 +22345 425 +2JJ34 654 +22234 234 +2JJJ3 623 +22223 658 +J2JJJ 987 +2JJJJ 456 +22222 604 diff --git a/2023/day7/part2.py b/2023/day7/part2.py new file mode 100644 index 0000000..86b19c2 --- /dev/null +++ b/2023/day7/part2.py @@ -0,0 +1,201 @@ +#!/bin/python3 + +import re +import sys +from pprint import pprint +from collections import Counter + +input_f = sys.argv[1] + +d =[] +rank = 0 +#prev 250197359 +strength = 'AKQT98765432J' +print(strength) + +with open(input_f) as file: + for line in file: + d.append(line.split()) +pprint(d) +print() + +def same_kind(x): + s = Counter(x) + for i in s.values(): + if i == 4: + return True + return False + +def five_of_kind(x): + if len(Counter(x)) == 1: + return True + return False + +def full_house(x): + #print('full_house') + s = Counter(x) + if len(s) != 2: + return False + else: + tmp = list(s.values()) + if (tmp[0] == 2 or tmp[0] == 3) and (tmp[1] == 2 or tmp[1] == 3): + return True + else: + return False + +def three_of_kind(x): + s = Counter(x) + for i in s.values(): + if i == 3: + return True + return False + +def pairs(x): + s = Counter(x) + pairs = 0 + for i in s.values(): + if i == 2: + pairs += 1 + return pairs + +def c_pairs(x): + pairs = 0 + for i in x.values(): + if i == 2: + pairs += 1 + return pairs + +for idx,i in enumerate(d): + d[idx].append(rank) + card = d[idx][0] + tmp = Counter(card) + if card.find('J') != -1: + print(Counter(card)) + c = list(Counter(card)) + l = list(Counter(card).keys()) + if list(tmp.most_common()[0])[0] == 'J' and list(tmp.most_common()[0])[1] == 5: + print(tmp) + continue + v = tmp['J'] + del tmp['J'] + h = list(tmp.most_common()[0])[0] + for j in range(0,v): + tmp.update(h) + print(tmp) + print() + if len(Counter(tmp)) == 1: + d[idx][2] = 7 + continue + if list(Counter(tmp).most_common()[0])[1] == 4: + d[idx][2] = 6 + continue + if list(Counter(tmp).most_common()[0])[1] == 3 and list(Counter(tmp).most_common()[1])[1] == 2: + d[idx][2] = 5 + continue + if list(Counter(tmp).most_common()[0])[1] == 3: + d[idx][2] = 4 + continue + if c_pairs(Counter(tmp)) == 2: + d[idx][2] = 3 + continue + if c_pairs(Counter(tmp)) == 1: + d[idx][2] = 2 + continue + if c_pairs(Counter(tmp)) == 0: + d[idx][2] = 1 + continue + else: + # Five of a kind, where all five cards have the same label: AAAAA + if five_of_kind(d[idx][0]) == True: + d[idx][2] = 7 + continue + + #Four of a kind, where four cards have the same label and one card has a different label: AA8AA + if same_kind(d[idx][0]) == True: + d[idx][2] = 6 + continue + + #Full house, where three cards have the same label, and the remaining two cards share a different label: 23332 + if full_house(d[idx][0]) == True: + d[idx][2] = 5 + continue + + #Three of a kind, where three cards have the same label, and the remaining two cards are each different from any other card in the hand: TTT98 + if three_of_kind(d[idx][0]) == True: + d[idx][2] = 4 + continue + + #Two pair, where two cards share one label, two other cards share a second label, and the remaining card has a third label: 23432 + if pairs(d[idx][0]) == 2: + d[idx][2] = 3 + continue + + #One pair, where two cards share one label, and the other three cards have a different label from the pair and each other: A23A4 + if pairs(d[idx][0]) == 1: + d[idx][2] = 2 + continue + + #High card, where all cards' labels are distinct: 23456 + if pairs(d[idx][0]) == 0: + d[idx][2] = 1 + continue +#d.sort() + +#d = sorted(d, key=lambda x: x[2]) + +def pp(x): + for p in x: + for q in p: + print(q,end='\t') + print() + +x_1 = [] +x_2 = [] +x_3 = [] +x_4 = [] +x_5 = [] +x_6 = [] +x_7 = [] + +for y in d: + if y[2] == 1: + x_1.append(y+[[strength.index(c) for c in y[0]]]) + if y[2] == 2: + x_2.append(y+[[strength.index(c) for c in y[0]]]) + if y[2] == 3: + x_3.append(y+[[strength.index(c) for c in y[0]]]) + if y[2] == 4: + x_4.append(y+[[strength.index(c) for c in y[0]]]) + if y[2] == 5: + x_5.append(y+[[strength.index(c) for c in y[0]]]) + if y[2] == 6: + x_6.append(y+[[strength.index(c) for c in y[0]]]) + if y[2] == 7: + x_7.append(y+[[strength.index(c) for c in y[0]]]) + + +x_1 = sorted(x_1, key=lambda x: x[3],reverse=True) +x_2 = sorted(x_2, key=lambda x: x[3],reverse=True) +x_3 = sorted(x_3, key=lambda x: x[3],reverse=True) +x_4 = sorted(x_4, key=lambda x: x[3],reverse=True) +x_5 = sorted(x_5, key=lambda x: x[3],reverse=True) +x_6 = sorted(x_6, key=lambda x: x[3],reverse=True) +x_7 = sorted(x_7, key=lambda x: x[3],reverse=True) + +e = [] + +X = [x_1,x_2,x_3,x_4,x_5,x_6,x_7] + +for n in X: + for m in n: + e.append(m) + +pprint(e) + +result = 0 + +for idx,i in enumerate(e): + x = i[1] + result += (int(x) * (idx+1)) + +print(result)