generated from alvesvaren/AoC-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path18.py
More file actions
106 lines (81 loc) · 2.12 KB
/
18.py
File metadata and controls
106 lines (81 loc) · 2.12 KB
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
102
103
104
105
106
import numpy as np
from aoc import get_input
from functools import lru_cache
from tqdm import tqdm
total_area = 0
cubes = []
min_x = 100
max_x = -100
min_y = 100
max_y = -100
min_z = 100
max_z = -100
for cube in open("18.txt"):
x = int(cube.split(",")[0])
if(x > max_x - 1):
max_x = x + 1
elif(x < min_x + 1):
min_x = x - 1
y = int(cube.split(",")[1])
if(y > max_y - 1):
max_y = y + 1
elif(y < min_y + 1):
min_y = y - 1
z = int(cube.split(",")[2])
if(z > max_z - 1):
max_z = z + 1
elif(z < min_z + 1):
min_z = z - 1
cubes.append((x, y, z))
for cube in tqdm(cubes):
area = 6
if (cube[0] - 1, cube[1], cube[2]) in cubes:
area -= 1
if (cube[0] + 1, cube[1], cube[2]) in cubes:
area -= 1
if (cube[0], cube[1] - 1, cube[2]) in cubes:
area -= 1
if (cube[0], cube[1] + 1, cube[2]) in cubes:
area -= 1
if (cube[0], cube[1], cube[2] - 1) in cubes:
area -= 1
if (cube[0], cube[1], cube[2] + 1) in cubes:
area -= 1
total_area += area
print(total_area)
@lru_cache(None)
def dfs(pos):
# do a DFS
stack = [pos]
seen = set()
if pos in cubes:
return False
while len(stack) > 0:
pop = stack.pop()
if pop in cubes:
continue
for coord in range(3):
if not (0 <= pop[coord] <= 20):
return True
if pop in seen:
continue
seen.add(pop)
for coord in range(3):
dpos = np.array([0, 0, 0])
dpos[coord] = 1
dneg = np.array([0, 0, 0])
dneg[coord] = -1
stack.append(tuple(pop + dpos))
stack.append(tuple(pop + dneg))
return False
total_area = 0
for x, y, z in tqdm(cubes):
pos = np.array((x, y, z))
for coord in range(3):
dpos = np.array([0, 0, 0])
dpos[coord] = 1
dneg = np.array([0, 0, 0])
dneg[coord] = -1
for nbr in [tuple(pos + dpos), tuple(pos + dneg)]:
total_area += dfs(nbr)
print(total_area)