Skip to content

Commit

Permalink
Merge pull request #91 from ephemient/py/day12
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient authored Dec 12, 2024
2 parents 31ed7fe + f1a0431 commit 6511e64
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ Development occurs in language-specific directories:
|[Day9.hs](hs/src/Day9.hs)|[Day9.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day9.kt)|[day9.py](py/aoc2024/day9.py)|[day9.rs](rs/src/day9.rs)|
|[Day10.hs](hs/src/Day10.hs)|[Day10.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day10.kt)|[day10.py](py/aoc2024/day10.py)|[day10.rs](rs/src/day10.rs)|
|[Day11.hs](hs/src/Day11.hs)|[Day11.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day11.kt)|[day11.py](py/aoc2024/day11.py)|[day11.rs](rs/src/day11.rs)|
|[Day12.hs](hs/src/Day12.hs)|[Day12.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day12.kt)|||
|[Day12.hs](hs/src/Day12.hs)|[Day12.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day12.kt)|[day12.py](py/aoc2024/day12.py)||
146 changes: 146 additions & 0 deletions py/aoc2024/day12.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
"""
Day 12: Garden Groups
"""

from collections import Counter, defaultdict
import textwrap
from typing import Generator

SAMPLE_INPUT_1 = textwrap.dedent(
"""\
AAAA
BBCD
BBCC
EEEC
"""
)
SAMPLE_INPUT_2 = textwrap.dedent(
"""\
OOOOO
OXOXO
OOOOO
OXOXO
OOOOO
"""
)
SAMPLE_INPUT_3 = textwrap.dedent(
"""\
RRRRIICCFF
RRRRIICCCF
VVRRRCCFFF
VVRCCCJFFF
VVVVCJJCFE
VVIVCCJJEE
VVIIICJJEE
MIIIIIJJEE
MIIISIJEEE
MMMISSJEEE
"""
)
SAMPLE_INPUT_4 = textwrap.dedent(
"""\
EEEEE
EXXXX
EEEEE
EXXXX
EEEEE
"""
)
SAMPLE_INPUT_5 = textwrap.dedent(
"""\
AAAAAA
AAABBA
AAABBA
ABBAAA
ABBAAA
AAAAAA
"""
)


def _groups(data: str) -> Generator[set[tuple[int, int]]]:
lines = data.splitlines()
visited = set()
for y, line in enumerate(lines):
for x, char in enumerate(line):
group = set()
stack = [(y, x)]
while stack:
y2, x2 = pos = stack.pop()
if lines[y2][x2] != char or pos in visited:
continue
visited.add(pos)
group.add(pos)
if 0 < y2:
stack.append((y2 - 1, x2))
if 0 < x2:
stack.append((y2, x2 - 1))
if x2 < len(lines[y2]) - 1:
stack.append((y2, x2 + 1))
if y2 < len(lines) - 1:
stack.append((y2 + 1, x2))
if group:
yield (group)


def _perimeter1(group: set[tuple[int, int]]) -> int:
edges = Counter()
for y, x in group:
edges.update(
(
(2 * y - 1, 2 * x),
(2 * y, 2 * x - 1),
(2 * y, 2 * x + 1),
(2 * y + 1, 2 * x),
)
)
return sum(value == 1 for value in edges.values())


def part1(data: str) -> int:
"""
>>> part1(SAMPLE_INPUT_1)
140
>>> part1(SAMPLE_INPUT_2)
772
>>> part1(SAMPLE_INPUT_3)
1930
"""
return sum(len(group) * _perimeter1(group) for group in _groups(data))


def _perimeter2(group: set[tuple[int, int]]) -> int:
edges = defaultdict(set)
for y, x in group:
edges[(2 * y - 1, 2 * x)].add(0)
edges[(2 * y, 2 * x - 1)].add(1)
edges[(2 * y, 2 * x + 1)].add(3)
edges[(2 * y + 1, 2 * x)].add(2)
lines = defaultdict(set)
for (y, x), value in edges.items():
if len(value) == 1:
value = next(iter(value))
lines[x if value % 2 else y + 1].add((x + y, value))
return sum(
1 + sum(abs(q - p) > 2 or b != d for (q, b), (p, d) in zip(line, line[1:]))
for line in map(sorted, lines.values())
)


def part2(data: str) -> int:
"""
>>> part2(SAMPLE_INPUT_1)
80
>>> part2(SAMPLE_INPUT_2)
436
>>> part2(SAMPLE_INPUT_4)
236
>>> part2(SAMPLE_INPUT_5)
368
>>> part2(SAMPLE_INPUT_3)
1206
"""
return sum(len(group) * _perimeter2(group) for group in _groups(data))


parts = (part1, part2)
1 change: 1 addition & 0 deletions py/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ day8 = "aoc2024.day8:parts"
day9 = "aoc2024.day9:parts"
day10 = "aoc2024.day10:parts"
day11 = "aoc2024.day11:parts"
day12 = "aoc2024.day12:parts"

[build-system]
requires = ["poetry-core"]
Expand Down

0 comments on commit 6511e64

Please sign in to comment.