Refactored Day07 a bit
This commit is contained in:
parent
6b0928e4b7
commit
9b52205b8f
1 changed files with 40 additions and 62 deletions
102
aoc/day07.py
102
aoc/day07.py
|
@ -1,10 +1,10 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from enum import Enum
|
||||
from collections import defaultdict
|
||||
from functools import reduce
|
||||
from enum import Enum
|
||||
from typing import Optional
|
||||
|
||||
from .aoc import AocParseLinesSameParser, Aoc2
|
||||
from .aoc import Aoc2
|
||||
|
||||
|
||||
class Type(Enum):
|
||||
|
@ -19,20 +19,29 @@ class Type(Enum):
|
|||
def __lt__(self, other: Type) -> bool:
|
||||
return self.value < other.value
|
||||
|
||||
class Hand2:
|
||||
def __init__(self, string: str) -> None:
|
||||
translation = "J23456789TQKA"
|
||||
|
||||
class Hand:
|
||||
def __init__(
|
||||
self, string: str, translation: str, joker: Optional[str] = None
|
||||
) -> None:
|
||||
self.string = [translation.index(x) for x in string]
|
||||
self.cards_dict = defaultdict(lambda : 0)
|
||||
self.cards_dict: dict[str, int] = defaultdict(lambda: 0)
|
||||
for letter in string:
|
||||
self.cards_dict[letter] += 1
|
||||
|
||||
value_amount = list(reversed(sorted(list([v for k, v in self.cards_dict.items() if k !="J"]))))
|
||||
joker_amount = self.cards_dict["J"]
|
||||
value_amount = list(
|
||||
reversed(
|
||||
sorted(
|
||||
list([v for k, v in self.cards_dict.items() if k != joker]))
|
||||
)
|
||||
)
|
||||
joker_amount = 0
|
||||
if joker is not None:
|
||||
joker_amount = self.cards_dict[joker]
|
||||
|
||||
if joker_amount == 5:
|
||||
self.type = Type.FIVE_KIND
|
||||
return
|
||||
if joker_amount == 5:
|
||||
self.type = Type.FIVE_KIND
|
||||
return
|
||||
|
||||
match value_amount[0] + joker_amount:
|
||||
case 5:
|
||||
|
@ -52,73 +61,42 @@ class Hand2:
|
|||
case _:
|
||||
self.type = Type.HIGH_CARD
|
||||
|
||||
|
||||
|
||||
def __lt__(self, other: Hand) -> bool:
|
||||
""" self < other """
|
||||
return (self.type, self.string) < (other.type, other.string)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"Hand({self.type}, {self.string})"
|
||||
|
||||
class Hand:
|
||||
def __init__(self, string: str) -> None:
|
||||
translation = "23456789TJQKA"
|
||||
self.string = [translation.index(x) for x in string]
|
||||
self.cards_dict = defaultdict(lambda : 0)
|
||||
for letter in string:
|
||||
self.cards_dict[letter] += 1
|
||||
|
||||
value_amount = list(reversed(sorted(list(self.cards_dict.values()))))
|
||||
|
||||
match value_amount[0]:
|
||||
case 5:
|
||||
self.type = Type.FIVE_KIND
|
||||
case 4:
|
||||
self.type = Type.FOUR_KIND
|
||||
case 3:
|
||||
if value_amount[1] == 2:
|
||||
self.type = Type.FULL_HOUSE
|
||||
else:
|
||||
self.type = Type.THREE_KIND
|
||||
case 2:
|
||||
if value_amount[1] == 2:
|
||||
self.type = Type.TWO_PAIR
|
||||
else:
|
||||
self.type = Type.ONE_PAIR
|
||||
case _:
|
||||
self.type = Type.HIGH_CARD
|
||||
|
||||
|
||||
|
||||
def __lt__(self, other: Hand) -> bool:
|
||||
""" self < other """
|
||||
"""self < other"""
|
||||
return (self.type, self.string) < (other.type, other.string)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"Hand({self.type}, {self.string})"
|
||||
|
||||
|
||||
# class Day07(AocParseLinesSameParser[tuple[Hand, int]], Aoc2[list[tuple[Hand, int]], list[tuple[Hand, int]]]):
|
||||
class Day07(Aoc2[list[tuple[Hand, int]], list[tuple[Hand2, int]]]):
|
||||
class Day07(Aoc2[list[tuple[Hand, int]], list[tuple[Hand, int]]]):
|
||||
def parseinput1(self, inpt: str) -> list[tuple[Hand, int]]:
|
||||
lines = inpt.splitlines()
|
||||
return [(Hand(line.split(" ")[0]), int(line.split(" ")[1])) for line in lines]
|
||||
return [
|
||||
(Hand(line.split(" ")[0], "23456789TJQKA"),
|
||||
int(line.split(" ")[1]))
|
||||
for line in lines
|
||||
]
|
||||
|
||||
def parseinput2(self, inpt: str) -> list[tuple[Hand2, int]]:
|
||||
def parseinput2(self, inpt: str) -> list[tuple[Hand, int]]:
|
||||
lines = inpt.splitlines()
|
||||
return [(Hand2(line.split(" ")[0]), int(line.split(" ")[1])) for line in lines]
|
||||
return [
|
||||
(Hand(line.split(" ")[0], "J23456789TQKA", "J"), int(
|
||||
line.split(" ")[1]))
|
||||
for line in lines
|
||||
]
|
||||
|
||||
def part1(self, inpt: list[tuple[Hand, int]]) -> int:
|
||||
return sum(((rnk + 1) * bid) for rnk, (_, bid) in list(enumerate(sorted(inpt))))
|
||||
def part1(self, inpt: list[tuple[Hand, int]]) -> int:
|
||||
return sum(((rnk + 1) * bid) for rnk, (_, bid) in list(enumerate(sorted(inpt))))
|
||||
|
||||
def part2(self, inpt: list[tuple[Hand, int]]) -> int:
|
||||
return sum(((rnk + 1) * bid) for rnk, (_, bid) in list(enumerate(sorted(inpt))))
|
||||
|
||||
def part2(self, inpt: list[tuple[Hand2, int]]) -> int:
|
||||
return sum(((rnk + 1) * bid) for rnk, (_, bid) in list(enumerate(sorted(inpt))))
|
||||
|
||||
def main() -> None:
|
||||
day07 = Day07(2023, 7)
|
||||
day07.run()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue