-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart1.py
60 lines (45 loc) · 1.71 KB
/
part1.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
from collections import namedtuple, defaultdict
Cuboid = namedtuple('Cuboid', ['min_x', 'max_x', 'min_y', 'max_y', 'min_z', 'max_z'])
with open("input.txt") as file:
reboot_steps = []
for line in filter(None, map(str.strip, file)):
switch, coords = line.split()
x, y, z = (
list(map(int, coord[2:].split('..')))
for coord in coords.split(',')
)
reboot_steps.append((
Cuboid(*x, *y, *z), (1 if switch == 'on' else -1)
))
cuboids = defaultdict(int)
for cuboid, mul in reboot_steps:
if min(cuboid.min_x, cuboid.min_y, cuboid.min_z) < -50 or max(cuboid.max_x, cuboid.max_y, cuboid.max_z) > 50:
continue
new_cuboids = cuboids.copy()
if mul > 0:
new_cuboids[cuboid] += mul
for prev_cuboid in cuboids:
if prev_cuboid.min_x > cuboid.max_x or cuboid.min_x > prev_cuboid.max_x:
continue
if prev_cuboid.min_y > cuboid.max_y or cuboid.min_y > prev_cuboid.max_y:
continue
if prev_cuboid.min_z > cuboid.max_z or cuboid.min_z > prev_cuboid.max_z:
continue
new_cuboids[Cuboid(
max(prev_cuboid.min_x, cuboid.min_x),
min(prev_cuboid.max_x, cuboid.max_x),
max(prev_cuboid.min_y, cuboid.min_y),
min(prev_cuboid.max_y, cuboid.max_y),
max(prev_cuboid.min_z, cuboid.min_z),
min(prev_cuboid.max_z, cuboid.max_z),
)] -= cuboids[prev_cuboid]
cuboids = new_cuboids
volume = 0
for cuboid, mul in cuboids.items():
volume += (
(cuboid.max_x - cuboid.min_x + 1)
* (cuboid.max_y - cuboid.min_y + 1)
* (cuboid.max_z - cuboid.min_z + 1)
* mul
)
print(volume)