summaryrefslogtreecommitdiffstats
path: root/prog/aoc/23/7/1.py
diff options
context:
space:
mode:
authorAnton Luka Šijanec <anton@sijanec.eu>2023-12-08 08:00:46 +0100
committerAnton Luka Šijanec <anton@sijanec.eu>2023-12-08 08:00:46 +0100
commit842b1eda1072fdabff2c99ba551753ba4bede04a (patch)
tree82bbc230c65cc447289df6eff22797893f685505 /prog/aoc/23/7/1.py
parentaoc6 (diff)
downloadr-842b1eda1072fdabff2c99ba551753ba4bede04a.tar
r-842b1eda1072fdabff2c99ba551753ba4bede04a.tar.gz
r-842b1eda1072fdabff2c99ba551753ba4bede04a.tar.bz2
r-842b1eda1072fdabff2c99ba551753ba4bede04a.tar.lz
r-842b1eda1072fdabff2c99ba551753ba4bede04a.tar.xz
r-842b1eda1072fdabff2c99ba551753ba4bede04a.tar.zst
r-842b1eda1072fdabff2c99ba551753ba4bede04a.zip
Diffstat (limited to '')
-rwxr-xr-xprog/aoc/23/7/1.py84
1 files changed, 84 insertions, 0 deletions
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)