-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday12.py
85 lines (62 loc) · 2.1 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
from point import CARDINAL_DIRECTIONS, E, N, Point, S, W, add_points, neighbors
from utils import one_of, with_lines
Region = set[Point]
@with_lines
def day12(lines):
garden = parse(lines)
regions: list[Region] = []
region_map: dict[Point, Region] = dict()
for p, c in garden.items():
if p in region_map:
continue
queue = [p]
region = set()
regions.append(region)
while queue:
q = queue.pop()
if q in region:
continue
region.add(q)
region_map[q] = region
for n in neighbors(q):
if n in garden and garden[n] == c:
queue.append(n)
result_a = sum(price(r) for r in regions)
result_b = sum(bulk_price(r) for r in regions)
for r in regions:
p = one_of(r)
print(f'Region {garden[p]} starting at {p} with area {area(r)} and sides {sides(r)}')
return result_a, result_b
def bulk_price(region: Region) -> int:
return area(region) * sides(region)
def price(region: Region) -> int:
return area(region) * perimeter(region)
def area(region: Region) -> int:
return len(region)
def perimeter(region: Region) -> int:
return sum(sum(1 for n in neighbors(p) if n not in region) for p in region)
NEXT_DIRECTION = {
N: E,
E: S,
S: W,
W: N
}
def sides(region: Region) -> int:
# like perimeter but if my neighbor to the E has a N fence, my N fence doesn't count
# For N fence -> E neighbor, for E fence -> S neighbor, for S fence -> W neighbor, for W fence -> N neighbor
num_sides = 0
for p in region:
for d in CARDINAL_DIRECTIONS:
e = NEXT_DIRECTION[d]
n = add_points(p, e)
if n in region and add_points(n, d) not in region:
continue
if add_points(p, d) not in region:
num_sides += 1
return num_sides
def parse(lines: list[str]) -> dict[Point, str]:
garden = {}
for y, line in enumerate(lines):
for x, c in enumerate(line):
garden[Point(x, y)] = c
return garden