-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday12_1.py
54 lines (40 loc) · 1.32 KB
/
day12_1.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
from collections import defaultdict, deque
import time
start_time = time.time()
is_test = False
day = 12
input_file = f'./input/day{day}{"_test" if is_test else ""}.txt'
dirs = ((-1, 0), (1, 0), (0, -1), (0, 1))
def build_grid(ls):
return defaultdict(str) | {(i, j): ls[i][j] for i in range(len(ls)) for j in range(len(ls[i]))}, len(ls), len(ls[0])
g, m, n = build_grid(open(input_file, 'r').read().splitlines())
def find_non_adj(grid, r, c):
count = 0
for dx, dy in dirs:
nr, nc = r + dx, c + dy
if grid[nr, nc] and grid[nr, nc] == grid[r, c]:
count += 1
return 4 - count
def solve(grid):
visited = set()
total = 0
def dfs(r, c, path):
path.add((r, c))
visited.add((r, c))
for dx, dy in dirs:
nr, nc = r + dx, c + dy
if grid[nr, nc] and grid[nr, nc] == grid[r, c] and (r + dx, c + dy) not in visited:
visited.add((nr, nc))
dfs(nr, nc, path)
for r in range(m):
for c in range(n):
if (r, c) not in visited:
path = set()
dfs(r, c, path)
area = len(path)
per = 0
for i, j in path:
per += find_non_adj(grid, i, j)
total += area * per
print('total', total)
solve(g)