-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday12.py
101 lines (78 loc) · 2.48 KB
/
day12.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"""
Advent of Code 2024, Day 12: Garden Groups.
See: https://adventofcode.com/2024/day/12
"""
import sys
from typing import TextIO
from queue import SimpleQueue
from grid import Grid, NORTH, EAST, SOUTH, WEST
def part_one(file: TextIO) -> int:
"""
Solve part one of the puzzle.
"""
grid = Grid.from_ascii_grid(file.read())
regions, perimeters = [], {}
queue = SimpleQueue()
for p in grid:
if p in perimeters:
continue
region = []
queue.put(p)
while not queue.empty():
p_ = queue.get()
if p_ in perimeters:
continue
region.append(p_)
perimeters[p_] = 4
for n in grid.get_neighbors(p_):
if grid[n] == grid[p_]:
perimeters[p_] -= 1
queue.put(n)
regions.append(region)
return sum(len(r) * sum(perimeters[p] for p in r) for r in regions)
def part_two(file: TextIO) -> int:
"""
Solve part two of the puzzle.
"""
grid = Grid.from_ascii_grid(file.read())
regions, corners = [], {}
queue = SimpleQueue()
for p in grid:
if p in corners:
continue
region = set()
queue.put(p)
while not queue.empty():
p_ = queue.get()
if p_ in corners:
continue
region.add(p_)
v = grid[p_]
corners[p_] = 0
for d1, d2 in [(NORTH, EAST), (NORTH, WEST), (SOUTH, EAST), (SOUTH, WEST)]:
# "Outer" corners
if grid.get(p_ + d1, None) != v and grid.get(p_ + d2, None) != v:
corners[p_] += 1
# "Inner" corners
if (
grid.get(p_ + d1, None) == v
and grid.get(p_ + d2, None) == v
and grid.get(p_ + d1 + d2, None) != v
):
corners[p_] += 1
for n in grid.get_neighbors(p_):
if grid[n] == grid[p_]:
queue.put(n)
regions.append(region)
return sum(len(r) * sum(corners[p] for p in r) for r in regions)
def main():
"""
The main entrypoint for the script.
"""
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()