From f1dbdaf1f269a24bd64d384d51b2353fa6623a88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niko=20B=C3=B6ckerman?= Date: Sun, 28 Jul 2024 19:32:41 +0000 Subject: [PATCH] Use Counter container type Replace uses of custom dict in cases where Counter container type is applicable. --- adventofcode/d4.py | 11 ++++++++--- adventofcode/d7.py | 37 +++++++++++++++++-------------------- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/adventofcode/d4.py b/adventofcode/d4.py index 4d417b4..9b041c1 100644 --- a/adventofcode/d4.py +++ b/adventofcode/d4.py @@ -1,3 +1,4 @@ +from collections import Counter from dataclasses import dataclass from typing import Iterable @@ -35,11 +36,15 @@ def p1(input_: str) -> int: def p2(input_str: str) -> int: d = list(_parse_input(input_str.splitlines())) - counts: dict[int, int] = {n + 1: 1 for n in range(len(d))} + counts: Counter[int] = Counter(range(1, len(d) + 1)) for cards in d: matches = len(set(cards.winning) & set(cards.own)) card_count = counts[cards.card_id] - for i in range(cards.card_id + 1, cards.card_id + 1 + matches): - counts[i] += card_count + counts.update( + { + i: card_count + for i in range(cards.card_id + 1, cards.card_id + 1 + matches) + } + ) return sum(v for v in counts.values()) diff --git a/adventofcode/d7.py b/adventofcode/d7.py index f48ba56..02928ae 100644 --- a/adventofcode/d7.py +++ b/adventofcode/d7.py @@ -1,4 +1,5 @@ import enum +from collections import Counter from dataclasses import dataclass from functools import total_ordering from types import NotImplementedType @@ -48,21 +49,19 @@ def p1(input_str: str) -> int: def classify_hand_type(cards: str) -> HandType: assert len(cards) == 5 - value_counts: dict[str, int] = {} - for c in cards: - value_counts[c] = value_counts.get(c, 0) + 1 + value_counts = Counter[str](cards) + counts = list(map(lambda x: x[1], value_counts.most_common(2))) - counts = sorted(value_counts.items(), key=lambda x: x[1], reverse=True) - if counts[0][1] == 5: + if counts[0] == 5: return HandType.FiveOfAKind - if counts[0][1] == 4: + if counts[0] == 4: return HandType.FourOfAKind - if counts[0][1] == 3: - if counts[1][1] == 2: + if counts[0] == 3: + if counts[1] == 2: return HandType.FullHouse return HandType.ThreeOfAKind - if counts[0][1] == 2: - if counts[1][1] == 2: + if counts[0] == 2: + if counts[1] == 2: return HandType.TwoPair return HandType.OnePair return HandType.HighCard @@ -95,9 +94,7 @@ def p2(input_str: str) -> int: def classify_hand_type(cards: str) -> HandType: assert len(cards) == 5 - value_counts: dict[str, int] = {} - for c in cards: - value_counts[c] = value_counts.get(c, 0) + 1 + value_counts = Counter[str](cards) jokers = value_counts.get("J", 0) value_counts.pop("J", None) @@ -105,17 +102,17 @@ def classify_hand_type(cards: str) -> HandType: if jokers == 5: return HandType.FiveOfAKind - counts = sorted(value_counts.items(), key=lambda x: x[1], reverse=True) - if counts[0][1] + jokers == 5: + counts = list(map(lambda x: x[1], value_counts.most_common(2))) + if counts[0] + jokers == 5: return HandType.FiveOfAKind - if counts[0][1] + jokers == 4: + if counts[0] + jokers == 4: return HandType.FourOfAKind - if counts[0][1] + jokers == 3: - if counts[1][1] == 2: + if counts[0] + jokers == 3: + if counts[1] == 2: return HandType.FullHouse return HandType.ThreeOfAKind - if counts[0][1] + jokers == 2: - if counts[1][1] == 2: + if counts[0] + jokers == 2: + if counts[1] == 2: return HandType.TwoPair return HandType.OnePair return HandType.HighCard