-
Notifications
You must be signed in to change notification settings - Fork 0
/
day22-python.py
137 lines (101 loc) · 3.54 KB
/
day22-python.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import dataclasses
import typing
@dataclasses.dataclass
class Cuboid:
x: range
y: range
z: range
def volume(self):
return len(self.x) * len(self.y) * len(self.z)
def __mul__(self, other) -> typing.Union['Cuboid', None]:
r = Cuboid(
range(max(self.x.start, other.x.start), min(self.x.stop, other.x.stop)),
range(max(self.y.start, other.y.start), min(self.y.stop, other.y.stop)),
range(max(self.z.start, other.z.start), min(self.z.stop, other.z.stop))
)
if r.x.start >= r.x.stop or r.y.start >= r.y.stop or r.z.start >= r.z.stop:
return Cuboid(range(0, 0), range(0, 0), range(0, 0))
return r
def __sub__(self, other: 'Cuboid') -> 'Shape':
if (self * other).volume() == 0:
return Shape([self])
cuboids = []
for e in other.exterior():
exterior_cuboid = self * e
if exterior_cuboid.volume() > 0:
cuboids.append(exterior_cuboid)
return Shape(cuboids)
def exterior(self):
c = 10 ** 9
yield Cuboid(
range(-c, self.x.start),
range(-c, c),
range(-c, c),
)
yield Cuboid(
range(self.x.stop, c),
range(-c, c),
range(-c, c),
)
yield Cuboid(
range(self.x.start, self.x.stop),
range(-c, self.y.start),
range(-c, c),
)
yield Cuboid(
range(self.x.start, self.x.stop),
range(self.y.stop, c),
range(-c, c),
)
yield Cuboid(
range(self.x.start, self.x.stop),
range(self.y.start, self.y.stop),
range(-c, self.z.start),
)
yield Cuboid(
range(self.x.start, self.x.stop),
range(self.y.start, self.y.stop),
range(self.z.stop, c),
)
@dataclasses.dataclass
class Shape:
cuboids: typing.List[Cuboid]
def __add__(self, other: typing.Union['Cuboid', 'Shape']):
if isinstance(other, Cuboid):
other = Shape([other])
return Shape([*self.cuboids, *(other - self).cuboids])
def __sub__(self, other: typing.Union['Cuboid', 'Shape']) -> 'Shape':
if isinstance(other, Cuboid):
cuboids = []
for c in self.cuboids:
cuboids.extend((c - other).cuboids)
return Shape(cuboids)
if isinstance(other, Shape):
new_shape = self
for c in other.cuboids:
new_shape = new_shape - c
return new_shape
def __mul__(self, other: 'Cuboid'):
cuboids = [c * other for c in self.cuboids]
return Shape([c for c in cuboids if c.volume() > 0])
def volume(self):
return sum(c.volume() for c in self.cuboids)
with open('input-day-22.txt') as f:
lines = f.readlines()
shape = Shape([])
for i, line in enumerate(lines):
if line.strip() == "":
continue
state, line = line.split(" ")
ranges = line.split(",")
ranges = [r.split("=")[1].split("..") for r in ranges]
ranges = [range(int(f), int(t) + 1) for f, t in ranges]
cuboid = Cuboid(*ranges)
print(f"{i} / {len(lines)}", state, len(shape.cuboids))
if state == "on":
shape += cuboid
if state == "off":
shape -= cuboid
shape_50 = shape * Cuboid(range(-50, 51), range(-50, 51), range(-50, 51))
print("Task 1:", shape_50.volume())
print("Task 2:", shape.volume())