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
|
from collections import deque
input = "data/12_2024.txt"
data = open(input).read().strip()
lines = data.split("\n")
rows = len(lines)
cols = len(lines[0])
p1, p2, vis = 0, 0, set()
dirs = [(-1, 0), (0, 1), (1, 0), (0, -1)]
for r in range(rows):
for c in range(cols):
if (r, c) in vis:
continue
q, dic = deque([(r, c)]), dict()
area, perim = 0, 0
while q:
r2, c2 = q.popleft()
if (r2, c2) in vis:
continue
vis.add((r2, c2))
area += 1
for dr, dc in dirs:
rr = r2 + dr
cc = c2 + dc
if 0 <= rr < rows and 0 <= cc < cols and lines[rr][cc] == lines[r2][c2]:
q.append((rr, cc))
else:
perim += 1
if (dr, dc) not in dic:
dic[(dr, dc)] = set()
dic[(dr, dc)].add((r2, c2))
sides = 0
for k, vs in dic.items():
vis_dic = set()
for pr, pc in vs:
if (pr, pc) not in vis_dic:
sides += 1
q = deque([(pr, pc)])
while q:
r2, c2 = q.popleft()
if (r2, c2) in vis_dic:
continue
vis_dic.add((r2, c2))
for dr, dc in dirs:
rr, cc = r2 + dr, c2 + dc
if (rr, cc) in vs:
q.append((rr, cc))
p1 += area * perim
p2 += area * sides
print(p1)
print(p2)
|