diff --git a/7/advent_2.py b/7/advent_2.py new file mode 100755 index 0000000..4c8d188 --- /dev/null +++ b/7/advent_2.py @@ -0,0 +1,105 @@ +#!/usr/bin/env python3.12 + +from functools import cmp_to_key + + +class HandClass: + def __init__(self, bid, cards): + self.bid = bid + self.cards = cards + self.cardnumbers = [] + self.numbers = {} + x = [*cards] + for i in x: + number = self.cardnumber(i) + self.cardnumbers.append(number) + if number not in self.numbers: + self.numbers[number] = 1 + else: + self.numbers[number] += 1 + x.sort(key=self.cardnumber, reverse=True) + self.sorted = x + self.strength = self.handstrength() + + def cardnumber(self, x: str): + if x.isdigit(): + return int(x) + if x == "T": + return 10 + if x == "J": + return 1 + if x == "Q": + return 12 + if x == "K": + return 13 + if x == "A": + return 14 + return 0 + + def handstrength(self): + print(self.numbers.values()) + if len(set(self.cardnumbers)) == 1: + return 6 + nc = sorted(list(self.numbers.values())) + if len(nc) == 2 and nc[1] == 4: + return 5 + if len(nc) == 2 and nc[1] == 3: + return 4 + if len(nc) == 3 and nc[2] == 3: + return 3 + if len(nc) == 3 and nc[2] == 2: + return 2 + if len(nc) == 4: + return 1 + return 0 + + def __str__(self): + return f"Cards: {self.cards} Bid: {self.bid}, sorted: {self.sorted}, num {self.cardnumbers}, numcount {self.numbers}, str: {self.strength}" + + def __repr__(self): + return str(self) + + +def compare(c1, c2): + if c1.strength > c2.strength: + return 1 + if c1.strength < c2.strength: + return -1 + # if c1.strength == 0 and c2.strength == 0: + # for i in range(0, len(c1.sorted)): + # if c1.sorted[i] > c2.sorted[i]: + # return 1 + # if c1.sorted[i] < c2.sorted[i]: + # return -1 + # return 0 + if c1.strength == c2.strength: + for i in range(0, len(c1.cardnumbers)): + if c1.cardnumbers[i] > c2.cardnumbers[i]: + return 1 + if c1.cardnumbers[i] < c2.cardnumbers[i]: + return -1 + return 0 + + +with open("data.txt") as file: + lines = [line.rstrip() for line in file] + +hands = [] +for line in lines: + halves = line.split(" ") + hands.append(HandClass(halves[1], halves[0])) + print(hands[-1]) + + +h2 = sorted(hands, key=cmp_to_key(compare), reverse=False) + + +print("sorditud") +total = 0 +count = 0 +for h in h2: + count += 1 + total += count*int(h.bid) + print(h) + +print(total)