Skip to content

Commit 16c2db0

Browse files
committed
y2023 d7
1 parent d34f6b7 commit 16c2db0

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

src/advent_of_code/y2023/d7.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,40 @@ class CamelCardsHand:
2626
@cached_property
2727
def hand_type_strength(self) -> int:
2828
card_value_counts = {
29-
v: sum(1 for c in self.cards if c == v) for v in set(self.cards)
29+
v: sum(1 for c in self.cards if c == v)
30+
for v in {card for card in self.cards if card != 0}
3031
}
3132
card_counts = card_value_counts.values()
3233

33-
if set(card_counts) == {5}:
34+
num_jokers = sum(1 for c in self.cards if c == 0)
35+
36+
if max(card_counts or [0]) + num_jokers == 5:
3437
return FIVE_OF_A_KIND
35-
elif set(card_counts) == {4, 1}:
38+
elif max(card_counts or [0]) + num_jokers == 4:
3639
return FOUR_OF_A_KIND
37-
elif set(card_counts) == {3, 2}:
40+
elif set(card_counts) == {3, 2} or (
41+
sorted(card_counts) == [2, 2] and num_jokers == 1
42+
):
3843
return FULL_HOUSE
39-
elif set(card_counts) == {3, 1}:
44+
elif max(card_counts or [0]) + num_jokers == 3:
4045
return THREE_OF_A_KIND
4146
elif sorted(card_counts) == [1, 2, 2]:
4247
return TWO_PAIR
43-
elif 2 in card_counts:
48+
elif 2 in card_counts or num_jokers == 1:
4449
return ONE_PAIR
4550
else:
4651
return HIGH_CARD
4752

48-
def __eq__(self, other: "CamelCardsHand") -> bool:
53+
def __eq__(self, other) -> bool:
54+
if not isinstance(other, CamelCardsHand):
55+
return NotImplemented
56+
4957
return self.cards == other.cards
5058

51-
def __lt__(self, other: "CamelCardsHand") -> bool:
59+
def __lt__(self, other) -> bool:
60+
if not isinstance(other, CamelCardsHand):
61+
return NotImplemented
62+
5263
if self.hand_type_strength < other.hand_type_strength:
5364
return True
5465
if self.hand_type_strength == other.hand_type_strength:
@@ -62,12 +73,12 @@ def __lt__(self, other: "CamelCardsHand") -> bool:
6273
return False
6374

6475
@classmethod
65-
def from_input(cls, input_line: str) -> "CamelCardsHand":
76+
def from_input(cls, input_line: str, joker_rule: bool = False) -> "CamelCardsHand":
6677
cards_part, bid_part = input_line.split()
6778

6879
values_map = {
6980
"T": 10,
70-
"J": 11,
81+
"J": 0 if joker_rule else 11,
7182
"Q": 12,
7283
"K": 13,
7384
"A": 14,
@@ -79,7 +90,7 @@ def from_input(cls, input_line: str) -> "CamelCardsHand":
7990

8091
return cls(
8192
bid=int(bid_part),
82-
cards=card_values,
93+
cards=card_values, # type: ignore
8394
)
8495

8596

@@ -90,7 +101,9 @@ def part_1(cards_input: list[str]) -> int:
90101

91102

92103
def part_2(cards_input: list[str]) -> int:
93-
pass
104+
hands = [CamelCardsHand.from_input(line, joker_rule=True) for line in cards_input]
105+
106+
return sum(i * hand.bid for i, hand in enumerate(sorted(hands), start=1))
94107

95108

96109
if __name__ == "__main__":

tests/test_y2023/test_d7.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ def test_part_1():
1414

1515

1616
def test_part_2():
17-
assert part_2(TEST_INPUT) == 0
17+
assert part_2(TEST_INPUT) == 5905

0 commit comments

Comments
 (0)