From 842b1eda1072fdabff2c99ba551753ba4bede04a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20Luka=20=C5=A0ijanec?= Date: Fri, 8 Dec 2023 08:00:46 +0100 Subject: aoc7 --- prog/aoc/23/7/1.py | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++ prog/aoc/23/7/in.txt | 5 ++++ 2 files changed, 89 insertions(+) create mode 100755 prog/aoc/23/7/1.py create mode 100644 prog/aoc/23/7/in.txt (limited to 'prog') diff --git a/prog/aoc/23/7/1.py b/prog/aoc/23/7/1.py new file mode 100755 index 0000000..e1d66da --- /dev/null +++ b/prog/aoc/23/7/1.py @@ -0,0 +1,84 @@ +#!/usr/bin/python3 +from functools import cmp_to_key +hands = [] +try: + while True: + hands.append(input().split(" ")) +except EOFError: + pass +alphabet = ["A", "K", "Q", "J", "T", "9", "8", "7", "6", "5", "4", "3", "2"]; +def level(a): + if len(set(a)) == 1: + return 6 + if len(set(a)) == 2: + if a.count(a[0]) == 1 or a.count(a[0]) == 4: + return 5 + return 4 + if len(set(a)) == 3: + if a.count(list(set(a))[0]) == 3 or a.count(list(set(a))[1]) == 3 or a.count(list(set(a))[2]) == 3: + return 3 + return 2 + if len(set(a)) == 5: + return 0 + return 1 +def compar(a, b): + a = a[0] + b = b[0] + if level(a) > level(b): + return 1 + if level(b) > level(a): + return -1 + for i in range(len(a)): + if alphabet.index(a[i]) < alphabet.index(b[i]): + return 1 + if alphabet.index(b[i]) < alphabet.index(a[i]): + return -1 + return 0 +hands.sort(key=cmp_to_key(compar)) +i = 1 +s = 0 +for h in hands: + print(h, level(h[0])) + s += int(h[1])*i + i += 1 +print(s) +alphabet = ["A", "K", "Q", "T", "9", "8", "7", "6", "5", "4", "3", "2", "J"]; +def level(a): + r = set(a)-set(["J"]) + if len(r) == 0 or len(r) == 1: + return 6 + if len(r) == 5: + return 0 + if len(r) == 2: + c2 = a.count("J")+a.count(list(r)[0]) + c1 = a.count("J")+a.count(list(r)[1]) + if c1 == 4 or c2 == 4: + return 5 + return 4 + if len(r) == 3: + l = a.count("J") + if l+a.count(list(set(r))[0]) == 3 or l+a.count(list(set(r))[1]) == 3 or l+a.count(list(set(r))[2]) == 3: + return 3 + return 2 + return 1 +def compar(a, b): + a = a[0] + b = b[0] + if level(a) > level(b): + return 1 + if level(b) > level(a): + return -1 + for i in range(len(a)): + if alphabet.index(a[i]) < alphabet.index(b[i]): + return 1 + if alphabet.index(b[i]) < alphabet.index(a[i]): + return -1 + return 0 +hands.sort(key=cmp_to_key(compar)) +i = 1 +s = 0 +for h in hands: + print(h, level(h[0])) + s += int(h[1])*i + i += 1 +print(s) diff --git a/prog/aoc/23/7/in.txt b/prog/aoc/23/7/in.txt new file mode 100644 index 0000000..e3500c3 --- /dev/null +++ b/prog/aoc/23/7/in.txt @@ -0,0 +1,5 @@ +32T3K 765 +T55J5 684 +KK677 28 +KTJJT 220 +QQQJA 483 -- cgit v1.2.3