Skip to content

Commit 2d758a6

Browse files
committed
AoC 2024 Day 12 - refactor
1 parent dcc841e commit 2d758a6

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

src/main/python/AoC2024_12.py

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66
import sys
77
from collections import defaultdict
8-
from typing import Callable
8+
from enum import Enum
9+
from enum import auto
10+
from enum import unique
911
from typing import Iterator
1012

1113
from aoc.common import InputData
@@ -68,13 +70,32 @@
6870
"""
6971

7072

73+
@unique
74+
class Pricing(Enum):
75+
PERIMETER = auto()
76+
NUMBER_OF_SIDES = auto()
77+
78+
def calculate(self, plot: Cell, region: set[Cell]) -> int:
79+
match self:
80+
case Pricing.PERIMETER:
81+
return 4 - sum(
82+
n in region for n in plot.get_capital_neighbours()
83+
)
84+
case Pricing.NUMBER_OF_SIDES:
85+
return sum(
86+
tuple(
87+
1 if plot.at(d[i]) in region else 0 for i in range(3)
88+
)
89+
in ((0, 0, 0), (1, 0, 0), (0, 1, 1))
90+
for d in CORNER_DIRS
91+
)
92+
93+
7194
class Solution(SolutionBase[Input, Output1, Output2]):
7295
def parse_input(self, input_data: InputData) -> Input:
7396
return input_data
7497

75-
def solve(
76-
self, input: Input, count: Callable[[Cell, set[Cell]], int]
77-
) -> int:
98+
def solve(self, input: Input, pricing: Pricing) -> int:
7899
def get_regions(input: Input) -> Iterator[set[Cell]]:
79100
plots_by_plant = defaultdict[str, set[Cell]](set)
80101
for r, row in enumerate(input):
@@ -94,25 +115,16 @@ def get_regions(input: Input) -> Iterator[set[Cell]]:
94115
all_plots_with_plant -= region
95116

96117
return sum(
97-
sum(count(plot, region) for plot in region) * len(region)
118+
sum(pricing.calculate(plot, region) for plot in region)
119+
* len(region)
98120
for region in get_regions(input)
99121
)
100122

101123
def part_1(self, input: Input) -> Output1:
102-
def count_edges(plot: Cell, region: set[Cell]) -> int:
103-
return 4 - sum(n in region for n in plot.get_capital_neighbours())
104-
105-
return self.solve(input, count_edges)
124+
return self.solve(input, Pricing.PERIMETER)
106125

107126
def part_2(self, input: Input) -> Output2:
108-
def count_corners(plot: Cell, region: set[Cell]) -> int:
109-
return sum(
110-
tuple(1 if plot.at(d[i]) in region else 0 for i in range(3))
111-
in ((0, 0, 0), (1, 0, 0), (0, 1, 1))
112-
for d in CORNER_DIRS
113-
)
114-
115-
return self.solve(input, count_corners)
127+
return self.solve(input, Pricing.NUMBER_OF_SIDES)
116128

117129
@aoc_samples(
118130
(

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy