-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday07.py
53 lines (36 loc) · 1.48 KB
/
day07.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
"""Advent of Code 2023 - Day 07."""
import collections
import sys
from typing import TextIO
def total(ordered_hands: list[list[str]]) -> int:
return sum(int(bid) * rank for rank, (_, bid) in enumerate(ordered_hands, start=1))
def part_one(file: TextIO) -> int:
def sort_hand(hand: list[str]) -> list[int]:
cards = hand[0]
card_counts = collections.Counter(cards)
max_count = max(card_counts.values())
card_values = list(map("23456789TJQKA".find, cards))
return [-len(card_counts), max_count, *card_values]
hands = list(map(lambda s: s.split(), file))
return total(sorted(hands, key=sort_hand))
def part_two(file: TextIO) -> int:
def sort_hand(hand: list[str]) -> list[int]:
cards = hand[0]
card_counts = collections.Counter(
cards.replace("J", "") if cards != "JJJJJ" else cards
)
max_count = (
max(card_counts.values(), default=0) + len(cards) - card_counts.total()
)
card_values = list(map("J23456789TQKA".find, cards))
return [-len(card_counts), max_count, *card_values]
hands = list(map(lambda s: s.split(), file))
return total(sorted(hands, key=sort_hand))
def main():
filename = sys.argv[0].replace(".py", ".txt")
with open(filename, encoding="utf-8") as file:
print("Part one:", part_one(file))
with open(filename, encoding="utf-8") as file:
print("Part two:", part_two(file))
if __name__ == "__main__":
main()