-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path07_Camel_Cards.py
108 lines (86 loc) · 2.75 KB
/
07_Camel_Cards.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from aoc import *
from collections import Counter
STRENGTH = list(
reversed(["A", "K", "Q", "J", "T", "9", "8", "7", "6", "5", "4", "3", "2"])
)
def score(hand):
return list(x for _, x in Counter(hand).most_common())
def joker_score(hand):
if "J" in hand:
return max(score(hand.replace("J", x)) for x in STRENGTH)
return score(hand)
def indices(items: Iterable, arr: list) -> list[int]:
return [arr.index(x) for x in items]
hands = [(str(h), b) for h, b in read()]
hands.sort(key=lambda x: (score(x[0]), indices(x[0], STRENGTH)))
print("Star 1:", sum((i + 1) * x[1] for i, x in enumerate(hands)))
STRENGTH.pop(STRENGTH.index("J"))
STRENGTH.insert(0, "J")
hands.sort(key=lambda x: (joker_score(x[0]), indices(x[0], STRENGTH)))
print("Star 2:", sum((i + 1) * x[1] for i, x in enumerate(hands)))
###############
# Visualisation
###############
from blessed import Terminal
import time
term = Terminal()
hands = [(str(h), b) for h, b in read()]
def kind(hand):
match score(hand):
case [5]:
return 0
case [4, 1]:
return 1
case [3, 2]:
return 2
case [3, 1, 1]:
return 3
case [2, 2, 1]:
return 4
case [2, 1, 1, 1]:
return 5
return 6
def print_with_kind(hand, kind):
match kind:
case 0:
print(term.bright_red(hand), end="")
case 1:
print(term.bright_magenta(hand), end="")
case 2:
print(term.bright_blue(hand), end="")
case 3:
print(term.bright_green(hand), end="")
case 4:
print(term.bright_yellow(hand), end="")
case 5:
print(term.bright_cyan(hand), end="")
case 6:
print(term.bright_white(hand), end="")
with term.fullscreen():
print(term.home(), end="")
print_with_kind("[Five of a kind]", 0)
print_with_kind(" | [Four of a kind]", 1)
print_with_kind(" | [Full house]", 2)
print_with_kind(" | [Three of a kind]", 3)
print_with_kind(" | [Two pair]", 4)
print_with_kind(" | [One pair]", 5)
print_with_kind(" | [High card]", 6)
print()
print()
with term.location():
for r in range(8):
for c in range(25):
print(hands[r * 25 + c][0], "", end="")
print()
time.sleep(1)
kinds = [kind(h[0]) for h in hands]
for cur_kind in range(7):
for r in range(8):
for c in range(25):
k = kinds[r * 25 + c]
if k == cur_kind:
with term.location():
print(term.move_xy(c * 6, r + 2), end="")
print_with_kind(hands[r * 25 + c][0] + " ", k)
time.sleep(0.025)
term.inkey()