From 06e58091d4b0352059d406e6274fda31b9d13b58 Mon Sep 17 00:00:00 2001 From: Stegallo Date: Fri, 15 Dec 2023 10:04:50 -0800 Subject: [PATCH 1/7] day6 --- y_2023/day6.py | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 y_2023/day6.py diff --git a/y_2023/day6.py b/y_2023/day6.py new file mode 100644 index 0000000..edf3058 --- /dev/null +++ b/y_2023/day6.py @@ -0,0 +1,62 @@ +from common.aoc import AoCDay +from typing import List, Optional +from pydantic.dataclasses import dataclass + + +@dataclass +class Placeholder: + name: str + orig_sequence: str + sequence: Optional[List[int]] = None + sequence_kernig: Optional[int] = None + + def __post_init__(self) -> None: + self.sequence = [int(i) for i in self.orig_sequence.split()] + self.sequence_kernig = int(self.orig_sequence.replace(" ", "")) + + @property + def size(self): + return len(self.sequence) + + +class Time(Placeholder): + ... + + +class Distance(Placeholder): + ... + + +class Day(AoCDay): + def __init__(self, test=0): + super().__init__(__name__, test) + + def _preprocess_input(self): + self.__input_data = self._input_data[0] + + def _calculate_1(self) -> int: # 227850 + time = Time(*self.__input_data[0].split(":")) + distance = Distance(*self.__input_data[1].split(":")) + + result = 1 + for i in range(time.size): + local_res = 0 + for j in range(time.sequence[i] + 1): # type: ignore + if j * (time.sequence[i] - j) > distance.sequence[i]: # type: ignore + local_res += 1 + + result *= local_res + return result + + def _calculate_2(self) -> int: # 42948149 + time = Time(*self.__input_data[0].split(":")) + distance = Distance(*self.__input_data[1].split(":")) + + result = 1 + local_res = 0 + for j in range(time.sequence_kernig + 1): # type: ignore + if j * (time.sequence_kernig - j) > distance.sequence_kernig: # type: ignore # noqa: E501 + local_res += 1 + + result *= local_res + return result From 715f53e2ceb63505e085f75b63e84493a98e82a0 Mon Sep 17 00:00:00 2001 From: Stegallo Date: Fri, 15 Dec 2023 14:08:28 -0800 Subject: [PATCH 2/7] day7 --- tests/y_2023/test_2023_day7.py | 64 +++++++++++ y_2023/day7.py | 187 +++++++++++++++++++++++++++++++++ 2 files changed, 251 insertions(+) create mode 100644 tests/y_2023/test_2023_day7.py create mode 100644 y_2023/day7.py diff --git a/tests/y_2023/test_2023_day7.py b/tests/y_2023/test_2023_day7.py new file mode 100644 index 0000000..169a439 --- /dev/null +++ b/tests/y_2023/test_2023_day7.py @@ -0,0 +1,64 @@ +from __future__ import annotations + +from unittest.mock import mock_open, patch + +from y_2023.day7 import Day, Hand + +with patch("builtins.open", mock_open(read_data="0")): + day = Day() + + +def test_Hand(): + print() + h = Hand("32T3K") + h = Hand("3333K") + print(h) + print(h.rate()) + assert True + + +def test_Hand_five(): + h = Hand("AAAAA") + assert h.rate() == ("Five of a kind", "A") + + +def test_Hand_four(): + h = Hand("AA8AA") + assert h.rate() == ("Four of a kind", "A") + + +def test_Hand_full(): + h = Hand("23332") + assert h.rate() == ("Full house", "3", "2") + + +def test_Hand_three(): + h = Hand("AA8A9") + assert h.rate() == ("Three of a kind", "A") + + +def test_Hand_two_pair(): + h = Hand("AA898") + assert h.rate() == ("Two pair", "A", "8") + + +def test_Hand_one_pair(): + h = Hand("AA8J9") + assert h.rate() == ("One pair", "A") + + +def test_Hand_high_card(): + h = Hand("23456") + assert h.rate() == ("High card", None) + + +def test__preprocess_input(): + assert True + + +def test_calculate_1(): + assert True + + +def test_calculate_2(): + assert True diff --git a/y_2023/day7.py b/y_2023/day7.py new file mode 100644 index 0000000..251e7cc --- /dev/null +++ b/y_2023/day7.py @@ -0,0 +1,187 @@ +from typing import List, Dict +from common.aoc import AoCDay +from pydantic.dataclasses import dataclass +from collections import Counter + +rank_type = { + "Five of a kind": 1, + "Four of a kind": 2, + "Full house": 3, + "Three of a kind": 4, + "Two pair": 5, + "One pair": 6, + "High card": 7, +} +rank_card = { + "A": 1, + "K": 2, + "Q": 3, + "J": 4, + "T": 5, + "9": 6, + "8": 7, + "7": 8, + "6": 9, + "5": 10, + "4": 11, + "3": 12, + "2": 13, +} +rank_card_2 = { + "A": 1, + "K": 2, + "Q": 3, + "T": 5, + "9": 6, + "8": 7, + "7": 8, + "6": 9, + "5": 10, + "4": 11, + "3": 12, + "2": 13, + "J": 14, +} + + +@dataclass +class Hand: + cards: str + # sort_ord: Optional[int] = None + # sort_ord_2: Optional[int] = None + + # def __post_init__(self) -> None: + # self.sort_ord = [rank_card[i] for i in self.cards] + # self.sort_ord_2 = [rank_card_2[i] for i in self.cards] + + @property + def sort_ord(self) -> List[int]: + return [rank_card[i] for i in self.cards] + + @property + def sort_ord_2(self) -> List[int]: + return [rank_card_2[i] for i in self.cards] + + def rate(self): + cou = Counter(self.cards) + if cou.most_common()[0][1] == 5: + # Five of a kind, where all five cards have the same label: AAAAA + return "Five of a kind", cou.most_common()[0][0] + if cou.most_common()[0][1] == 4: + # Four of a kind, where four cards have the same label and one card + # has a different label: AA8AA + return "Four of a kind", cou.most_common()[0][0] + if cou.most_common()[0][1] == 3 and cou.most_common()[1][1] == 2: + # Full house, where three cards have the same label, and the + # remaining two cards share a different label: 23332 + return "Full house", cou.most_common()[0][0], cou.most_common()[1][0] + if cou.most_common()[0][1] == 3: + # Full house, where three cards have the same label, and the + # remaining two cards share a different label: 23332 + return "Three of a kind", cou.most_common()[0][0] + if cou.most_common()[0][1] == 2 and cou.most_common()[1][1] == 2: + # Full house, where three cards have the same label, and the + # remaining two cards share a different label: 23332 + return "Two pair", cou.most_common()[0][0], cou.most_common()[1][0] + if cou.most_common()[0][1] == 2: + # Full house, where three cards have the same label, and the + # remaining two cards share a different label: 23332 + return "One pair", cou.most_common()[0][0] + + return "High card", None + + def rate2(self): + if "J" in self.cards: + # breakpoint() + str_clone = self.cards + cou = Counter(self.cards) + # print(cou) + if cou.most_common()[0][1] > 1 and cou.most_common()[0][0] != "J": + str_clone = str_clone.replace("J", cou.most_common()[0][0]) + if ( + cou.most_common()[0][1] > 1 + and cou.most_common()[0][0] == "J" + and cou.most_common()[0][1] < 5 + ): + str_clone = str_clone.replace("J", cou.most_common()[1][0]) + if cou.most_common()[0][1] == 1 and cou.most_common()[0][0] == "J": + str_clone = str_clone.replace("J", cou.most_common()[1][0]) + if cou.most_common()[0][1] == 1 and cou.most_common()[0][0] != "J": + str_clone = str_clone.replace("J", cou.most_common()[0][0]) + + cou = Counter(str_clone) + if cou.most_common()[0][1] == 5: + # Five of a kind, where all five cards have the same label: AAAAA + return "Five of a kind", cou.most_common()[0][0] + if cou.most_common()[0][1] == 4: + # Four of a kind, where four cards have the same label and one + # card has a different label: AA8AA + return "Four of a kind", cou.most_common()[0][0] + if cou.most_common()[0][1] == 3 and cou.most_common()[1][1] == 2: + # Full house, where three cards have the same label, and the + # remaining two cards share a different label: 23332 + return "Full house", cou.most_common()[0][0], cou.most_common()[1][0] + if cou.most_common()[0][1] == 3: + # Full house, where three cards have the same label, and the + # remaining two cards share a different label: 23332 + return "Three of a kind", cou.most_common()[0][0] + if cou.most_common()[0][1] == 2 and cou.most_common()[1][1] == 2: + # Full house, where three cards have the same label, and the + # remaining two cards share a different label: 23332 + return "Two pair", cou.most_common()[0][0], cou.most_common()[1][0] + if cou.most_common()[0][1] == 2: + # Full house, where three cards have the same label, and the + # remaining two cards share a different label: 23332 + return "One pair", cou.most_common()[0][0] + + return "High card", None + else: + return self.rate() + + +@dataclass +class Row: + hand: Hand + bid: int + + +class Day(AoCDay): + def __init__(self, test=0): + super().__init__(__name__, test) + + def _preprocess_input(self): + self.__input_data = [ + (lambda x, y: Row(Hand(x), y))(*i.split()) for i in self._input_data[0] + ] + + def _calculate_1(self) -> int: # 246424613 + ranks: Dict[str, List[Row]] = {i: [] for i in rank_type} + for i in self.__input_data: + ranks[i.hand.rate()[0]].append(i) + + final_ordering = [] + for i in ranks.values(): + final_ordering.extend(sorted([x for x in i], key=lambda x: x.hand.sort_ord)) + + result = 0 + for c, i in enumerate(final_ordering): + result += (len(final_ordering) - c) * int(i.bid) + + return result + + def _calculate_2(self) -> int: # 248256639 + ranks: Dict[str, List[Row]] = {i: [] for i in rank_type} + for i in self.__input_data: + ranks[i.hand.rate2()[0]].append(i) + + final_ordering = [] + for i in ranks.values(): + final_ordering.extend( + sorted([x for x in i], key=lambda x: x.hand.sort_ord_2), + ) + + result = 0 + for c, i in enumerate(final_ordering): + result += (len(final_ordering) - c) * int(i.bid) + + return result From 8fae0a3f92a759efb064caddd7d4f163a7e092e0 Mon Sep 17 00:00:00 2001 From: Stegallo Date: Fri, 15 Dec 2023 14:56:02 -0800 Subject: [PATCH 3/7] day7 --- y_2023/day7.py | 108 ++++++++++++++++++------------------------------- 1 file changed, 40 insertions(+), 68 deletions(-) diff --git a/y_2023/day7.py b/y_2023/day7.py index 251e7cc..2f0f0b5 100644 --- a/y_2023/day7.py +++ b/y_2023/day7.py @@ -3,7 +3,7 @@ from pydantic.dataclasses import dataclass from collections import Counter -rank_type = { +RANK_TYPE = { "Five of a kind": 1, "Four of a kind": 2, "Full house": 3, @@ -12,7 +12,7 @@ "One pair": 6, "High card": 7, } -rank_card = { +RANK_CARD = { "A": 1, "K": 2, "Q": 3, @@ -27,75 +27,54 @@ "3": 12, "2": 13, } -rank_card_2 = { - "A": 1, - "K": 2, - "Q": 3, - "T": 5, - "9": 6, - "8": 7, - "7": 8, - "6": 9, - "5": 10, - "4": 11, - "3": 12, - "2": 13, - "J": 14, -} +RANK_CARD_2 = {k: v if k != "J" else 14 for k, v in RANK_CARD.items()} @dataclass class Hand: cards: str - # sort_ord: Optional[int] = None - # sort_ord_2: Optional[int] = None - # def __post_init__(self) -> None: - # self.sort_ord = [rank_card[i] for i in self.cards] - # self.sort_ord_2 = [rank_card_2[i] for i in self.cards] + def sort_ord(self, type: str) -> List[int]: + if type == "original": + return [RANK_CARD[i] for i in self.cards] + if type == "joker": + return [RANK_CARD_2[i] for i in self.cards] + return [] @property - def sort_ord(self) -> List[int]: - return [rank_card[i] for i in self.cards] - - @property - def sort_ord_2(self) -> List[int]: - return [rank_card_2[i] for i in self.cards] - - def rate(self): + def __rate(self) -> str: cou = Counter(self.cards) if cou.most_common()[0][1] == 5: # Five of a kind, where all five cards have the same label: AAAAA - return "Five of a kind", cou.most_common()[0][0] + return "Five of a kind" # , cou.most_common()[0][0] if cou.most_common()[0][1] == 4: # Four of a kind, where four cards have the same label and one card # has a different label: AA8AA - return "Four of a kind", cou.most_common()[0][0] + return "Four of a kind" # , cou.most_common()[0][0] if cou.most_common()[0][1] == 3 and cou.most_common()[1][1] == 2: # Full house, where three cards have the same label, and the # remaining two cards share a different label: 23332 - return "Full house", cou.most_common()[0][0], cou.most_common()[1][0] + return "Full house" # , cou.most_common()[0][0], cou.most_common()[1][0] if cou.most_common()[0][1] == 3: # Full house, where three cards have the same label, and the # remaining two cards share a different label: 23332 - return "Three of a kind", cou.most_common()[0][0] + return "Three of a kind" # , cou.most_common()[0][0] if cou.most_common()[0][1] == 2 and cou.most_common()[1][1] == 2: # Full house, where three cards have the same label, and the # remaining two cards share a different label: 23332 - return "Two pair", cou.most_common()[0][0], cou.most_common()[1][0] + return "Two pair" # , cou.most_common()[0][0], cou.most_common()[1][0] if cou.most_common()[0][1] == 2: # Full house, where three cards have the same label, and the # remaining two cards share a different label: 23332 - return "One pair", cou.most_common()[0][0] + return "One pair" # , cou.most_common()[0][0] - return "High card", None + return "High card" # , None - def rate2(self): - if "J" in self.cards: - # breakpoint() + def rate(self, type: str) -> str: + if type == "joker" and "J" in self.cards: str_clone = self.cards cou = Counter(self.cards) - # print(cou) + if cou.most_common()[0][1] > 1 and cou.most_common()[0][0] != "J": str_clone = str_clone.replace("J", cou.most_common()[0][0]) if ( @@ -112,31 +91,33 @@ def rate2(self): cou = Counter(str_clone) if cou.most_common()[0][1] == 5: # Five of a kind, where all five cards have the same label: AAAAA - return "Five of a kind", cou.most_common()[0][0] + return "Five of a kind" # , cou.most_common()[0][0] if cou.most_common()[0][1] == 4: # Four of a kind, where four cards have the same label and one # card has a different label: AA8AA - return "Four of a kind", cou.most_common()[0][0] + return "Four of a kind" # , cou.most_common()[0][0] if cou.most_common()[0][1] == 3 and cou.most_common()[1][1] == 2: # Full house, where three cards have the same label, and the # remaining two cards share a different label: 23332 - return "Full house", cou.most_common()[0][0], cou.most_common()[1][0] + return ( + "Full house" # , cou.most_common()[0][0], cou.most_common()[1][0] + ) if cou.most_common()[0][1] == 3: # Full house, where three cards have the same label, and the # remaining two cards share a different label: 23332 - return "Three of a kind", cou.most_common()[0][0] + return "Three of a kind" # , cou.most_common()[0][0] if cou.most_common()[0][1] == 2 and cou.most_common()[1][1] == 2: # Full house, where three cards have the same label, and the # remaining two cards share a different label: 23332 - return "Two pair", cou.most_common()[0][0], cou.most_common()[1][0] + return "Two pair" # , cou.most_common()[0][0], cou.most_common()[1][0] if cou.most_common()[0][1] == 2: # Full house, where three cards have the same label, and the # remaining two cards share a different label: 23332 - return "One pair", cou.most_common()[0][0] + return "One pair" # , cou.most_common()[0][0] - return "High card", None + return "High card" # , None else: - return self.rate() + return self.__rate @dataclass @@ -154,30 +135,15 @@ def _preprocess_input(self): (lambda x, y: Row(Hand(x), y))(*i.split()) for i in self._input_data[0] ] - def _calculate_1(self) -> int: # 246424613 - ranks: Dict[str, List[Row]] = {i: [] for i in rank_type} + def __compute(self, type: str) -> int: + ranks: Dict[str, List[Row]] = {i: [] for i in RANK_TYPE} for i in self.__input_data: - ranks[i.hand.rate()[0]].append(i) - - final_ordering = [] - for i in ranks.values(): - final_ordering.extend(sorted([x for x in i], key=lambda x: x.hand.sort_ord)) - - result = 0 - for c, i in enumerate(final_ordering): - result += (len(final_ordering) - c) * int(i.bid) - - return result - - def _calculate_2(self) -> int: # 248256639 - ranks: Dict[str, List[Row]] = {i: [] for i in rank_type} - for i in self.__input_data: - ranks[i.hand.rate2()[0]].append(i) + ranks[i.hand.rate(type)].append(i) final_ordering = [] for i in ranks.values(): final_ordering.extend( - sorted([x for x in i], key=lambda x: x.hand.sort_ord_2), + sorted([x for x in i], key=lambda x: x.hand.sort_ord(type)), ) result = 0 @@ -185,3 +151,9 @@ def _calculate_2(self) -> int: # 248256639 result += (len(final_ordering) - c) * int(i.bid) return result + + def _calculate_1(self) -> int: # 246424613 + return self.__compute("original") + + def _calculate_2(self) -> int: # 248256639 + return self.__compute("joker") From 3e77a5a4b14bd3fa1e6db95b079bdfbfe333f22d Mon Sep 17 00:00:00 2001 From: Stegallo Date: Fri, 15 Dec 2023 14:59:44 -0800 Subject: [PATCH 4/7] test day7 --- tests/y_2023/test_2023_day7.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/y_2023/test_2023_day7.py b/tests/y_2023/test_2023_day7.py index 169a439..92b22ce 100644 --- a/tests/y_2023/test_2023_day7.py +++ b/tests/y_2023/test_2023_day7.py @@ -4,7 +4,7 @@ from y_2023.day7 import Day, Hand -with patch("builtins.open", mock_open(read_data="0")): +with patch("builtins.open", mock_open(read_data="0 1")): day = Day() @@ -13,43 +13,43 @@ def test_Hand(): h = Hand("32T3K") h = Hand("3333K") print(h) - print(h.rate()) + print(h.rate("original")) assert True def test_Hand_five(): h = Hand("AAAAA") - assert h.rate() == ("Five of a kind", "A") + assert h.rate("original") == ("Five of a kind") # , "A") def test_Hand_four(): h = Hand("AA8AA") - assert h.rate() == ("Four of a kind", "A") + assert h.rate("original") == ("Four of a kind") # , "A") def test_Hand_full(): h = Hand("23332") - assert h.rate() == ("Full house", "3", "2") + assert h.rate("original") == ("Full house") # , "3", "2") def test_Hand_three(): h = Hand("AA8A9") - assert h.rate() == ("Three of a kind", "A") + assert h.rate("original") == ("Three of a kind") # , "A") def test_Hand_two_pair(): h = Hand("AA898") - assert h.rate() == ("Two pair", "A", "8") + assert h.rate("original") == ("Two pair") # , "A", "8") def test_Hand_one_pair(): h = Hand("AA8J9") - assert h.rate() == ("One pair", "A") + assert h.rate("original") == ("One pair") # , "A") def test_Hand_high_card(): h = Hand("23456") - assert h.rate() == ("High card", None) + assert h.rate("original") == ("High card") # , None) def test__preprocess_input(): From 23478f6f299281503719ff2bc99fa5ff30d679ed Mon Sep 17 00:00:00 2001 From: Stegallo Date: Fri, 15 Dec 2023 15:31:46 -0800 Subject: [PATCH 5/7] sourcery config --- .sourcery.yaml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .sourcery.yaml diff --git a/.sourcery.yaml b/.sourcery.yaml new file mode 100644 index 0000000..e13f807 --- /dev/null +++ b/.sourcery.yaml @@ -0,0 +1,2 @@ +rule_settings: + disable: [reintroduce-else] From 55046db5f23fbcbcfb4a219df08399434db5c67b Mon Sep 17 00:00:00 2001 From: Stegallo Date: Fri, 15 Dec 2023 15:36:01 -0800 Subject: [PATCH 6/7] simplify J logic --- y_2023/day7.py | 89 ++++++++++++++++++++++++-------------------------- 1 file changed, 43 insertions(+), 46 deletions(-) diff --git a/y_2023/day7.py b/y_2023/day7.py index 2f0f0b5..9cba918 100644 --- a/y_2023/day7.py +++ b/y_2023/day7.py @@ -71,53 +71,50 @@ def __rate(self) -> str: return "High card" # , None def rate(self, type: str) -> str: - if type == "joker" and "J" in self.cards: - str_clone = self.cards - cou = Counter(self.cards) - - if cou.most_common()[0][1] > 1 and cou.most_common()[0][0] != "J": - str_clone = str_clone.replace("J", cou.most_common()[0][0]) - if ( - cou.most_common()[0][1] > 1 - and cou.most_common()[0][0] == "J" - and cou.most_common()[0][1] < 5 - ): - str_clone = str_clone.replace("J", cou.most_common()[1][0]) - if cou.most_common()[0][1] == 1 and cou.most_common()[0][0] == "J": - str_clone = str_clone.replace("J", cou.most_common()[1][0]) - if cou.most_common()[0][1] == 1 and cou.most_common()[0][0] != "J": - str_clone = str_clone.replace("J", cou.most_common()[0][0]) - - cou = Counter(str_clone) - if cou.most_common()[0][1] == 5: - # Five of a kind, where all five cards have the same label: AAAAA - return "Five of a kind" # , cou.most_common()[0][0] - if cou.most_common()[0][1] == 4: - # Four of a kind, where four cards have the same label and one - # card has a different label: AA8AA - return "Four of a kind" # , cou.most_common()[0][0] - if cou.most_common()[0][1] == 3 and cou.most_common()[1][1] == 2: - # Full house, where three cards have the same label, and the - # remaining two cards share a different label: 23332 - return ( - "Full house" # , cou.most_common()[0][0], cou.most_common()[1][0] - ) - if cou.most_common()[0][1] == 3: - # Full house, where three cards have the same label, and the - # remaining two cards share a different label: 23332 - return "Three of a kind" # , cou.most_common()[0][0] - if cou.most_common()[0][1] == 2 and cou.most_common()[1][1] == 2: - # Full house, where three cards have the same label, and the - # remaining two cards share a different label: 23332 - return "Two pair" # , cou.most_common()[0][0], cou.most_common()[1][0] - if cou.most_common()[0][1] == 2: - # Full house, where three cards have the same label, and the - # remaining two cards share a different label: 23332 - return "One pair" # , cou.most_common()[0][0] - - return "High card" # , None - else: + if type != "joker" or "J" not in self.cards: return self.__rate + str_clone = self.cards + cou = Counter(self.cards) + + if cou.most_common()[0][1] > 1 and cou.most_common()[0][0] != "J": + str_clone = str_clone.replace("J", cou.most_common()[0][0]) + if ( + cou.most_common()[0][1] > 1 + and cou.most_common()[0][0] == "J" + and cou.most_common()[0][1] < 5 + ): + str_clone = str_clone.replace("J", cou.most_common()[1][0]) + if cou.most_common()[0][1] == 1 and cou.most_common()[0][0] == "J": + str_clone = str_clone.replace("J", cou.most_common()[1][0]) + if cou.most_common()[0][1] == 1 and cou.most_common()[0][0] != "J": + str_clone = str_clone.replace("J", cou.most_common()[0][0]) + + cou = Counter(str_clone) + if cou.most_common()[0][1] == 5: + # Five of a kind, where all five cards have the same label: AAAAA + return "Five of a kind" # , cou.most_common()[0][0] + if cou.most_common()[0][1] == 4: + # Four of a kind, where four cards have the same label and one + # card has a different label: AA8AA + return "Four of a kind" # , cou.most_common()[0][0] + if cou.most_common()[0][1] == 3 and cou.most_common()[1][1] == 2: + # Full house, where three cards have the same label, and the + # remaining two cards share a different label: 23332 + return "Full house" # , cou.most_common()[0][0], cou.most_common()[1][0] + if cou.most_common()[0][1] == 3: + # Full house, where three cards have the same label, and the + # remaining two cards share a different label: 23332 + return "Three of a kind" # , cou.most_common()[0][0] + if cou.most_common()[0][1] == 2 and cou.most_common()[1][1] == 2: + # Full house, where three cards have the same label, and the + # remaining two cards share a different label: 23332 + return "Two pair" # , cou.most_common()[0][0], cou.most_common()[1][0] + if cou.most_common()[0][1] == 2: + # Full house, where three cards have the same label, and the + # remaining two cards share a different label: 23332 + return "One pair" # , cou.most_common()[0][0] + + return "High card" # , None @dataclass From 85351cf0e5bea045331590eb3929a57526ba8ee5 Mon Sep 17 00:00:00 2001 From: Sourcery AI <> Date: Fri, 15 Dec 2023 23:36:43 +0000 Subject: [PATCH 7/7] 'Refactored by Sourcery' --- tests/y_2023/test_2023_day7.py | 7 +++---- y_2023/day6.py | 20 ++++++++++---------- y_2023/day7.py | 13 +++++-------- 3 files changed, 18 insertions(+), 22 deletions(-) diff --git a/tests/y_2023/test_2023_day7.py b/tests/y_2023/test_2023_day7.py index 92b22ce..d60b454 100644 --- a/tests/y_2023/test_2023_day7.py +++ b/tests/y_2023/test_2023_day7.py @@ -14,7 +14,6 @@ def test_Hand(): h = Hand("3333K") print(h) print(h.rate("original")) - assert True def test_Hand_five(): @@ -53,12 +52,12 @@ def test_Hand_high_card(): def test__preprocess_input(): - assert True + pass def test_calculate_1(): - assert True + pass def test_calculate_2(): - assert True + pass diff --git a/y_2023/day6.py b/y_2023/day6.py index edf3058..27ede42 100644 --- a/y_2023/day6.py +++ b/y_2023/day6.py @@ -40,11 +40,11 @@ def _calculate_1(self) -> int: # 227850 result = 1 for i in range(time.size): - local_res = 0 - for j in range(time.sequence[i] + 1): # type: ignore - if j * (time.sequence[i] - j) > distance.sequence[i]: # type: ignore - local_res += 1 - + local_res = sum( + 1 + for j in range(time.sequence[i] + 1) + if j * (time.sequence[i] - j) > distance.sequence[i] + ) result *= local_res return result @@ -53,10 +53,10 @@ def _calculate_2(self) -> int: # 42948149 distance = Distance(*self.__input_data[1].split(":")) result = 1 - local_res = 0 - for j in range(time.sequence_kernig + 1): # type: ignore - if j * (time.sequence_kernig - j) > distance.sequence_kernig: # type: ignore # noqa: E501 - local_res += 1 - + local_res = sum( + 1 + for j in range(time.sequence_kernig + 1) + if j * (time.sequence_kernig - j) > distance.sequence_kernig + ) result *= local_res return result diff --git a/y_2023/day7.py b/y_2023/day7.py index 9cba918..f308eaf 100644 --- a/y_2023/day7.py +++ b/y_2023/day7.py @@ -139,15 +139,12 @@ def __compute(self, type: str) -> int: final_ordering = [] for i in ranks.values(): - final_ordering.extend( - sorted([x for x in i], key=lambda x: x.hand.sort_ord(type)), - ) + final_ordering.extend(sorted(list(i), key=lambda x: x.hand.sort_ord(type))) - result = 0 - for c, i in enumerate(final_ordering): - result += (len(final_ordering) - c) * int(i.bid) - - return result + return sum( + (len(final_ordering) - c) * int(i.bid) + for c, i in enumerate(final_ordering) + ) def _calculate_1(self) -> int: # 246424613 return self.__compute("original")