Skip to content

Commit

Permalink
Merge pull request #8 from nikobockerman:use_counter_type
Browse files Browse the repository at this point in the history
Use Counter container type
  • Loading branch information
nikobockerman authored Jul 28, 2024
2 parents fb0f697 + f1dbdaf commit 83874ca
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
11 changes: 8 additions & 3 deletions adventofcode/d4.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from collections import Counter
from dataclasses import dataclass
from typing import Iterable

Expand Down Expand Up @@ -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())
37 changes: 17 additions & 20 deletions adventofcode/d7.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import enum
from collections import Counter
from dataclasses import dataclass
from functools import total_ordering
from types import NotImplementedType
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -95,27 +94,25 @@ 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)

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
Expand Down

0 comments on commit 83874ca

Please sign in to comment.