From bb1ec64afc2a98b7f9fe7932d80d18e15ad06366 Mon Sep 17 00:00:00 2001 From: FrederikBaerentsen Date: Thu, 7 Dec 2023 15:58:07 +0100 Subject: [PATCH] Started 2023-12-07 --- 2023/day7/exp | 11 +++++ 2023/day7/part1.py | 117 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 2023/day7/exp diff --git a/2023/day7/exp b/2023/day7/exp new file mode 100644 index 0000000..1ab7a06 --- /dev/null +++ b/2023/day7/exp @@ -0,0 +1,11 @@ +32T3K 765 +T55J5 684 +AA8AA 21 +KK677 28 +AAAAA 123 +KTJJT 220 +23332 1 +A23A4 64 +QQQJA 483 +23456 64 +HNJIP 317 diff --git a/2023/day7/part1.py b/2023/day7/part1.py index b55b755..c13095a 100644 --- a/2023/day7/part1.py +++ b/2023/day7/part1.py @@ -3,11 +3,128 @@ import re import sys from pprint import pprint +from collections import Counter + input_f = sys.argv[1] d =[] +rank = 0 + +strength = 'AKQJT98765432' +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 + + +for idx,i in enumerate(d): + d[idx].append(rank) + + # 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]) + +print() +pprint(d) +print() +for idx,i in enumerate(d): + if idx == len(d)-1: + break + else: + if d[idx][2] == d[idx+1][2]: + #print('Comparing ' + str(d[idx][0]) + ' to ' + str(d[idx+1][0])) + tmp = list(d[idx][0]) + tmp2 = list(d[idx+1][0]) + for x in range(0,5): + if tmp[x] != tmp2[x]: + #print(tmp[x],tmp2[x]) + if strength.find(tmp[x]) < strength.find(tmp2[x]): + d[idx],d[idx+1] = d[idx+1],d[idx] + break + +result = 0 + +for idx,i in enumerate(d): + x = i[1] + result += (int(x) * (idx+1)) pprint(d) + +print(result)