You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.3 KiB

import re
def readInstructions(fileName):
instructions = []
with open(fileName) as file:
lines = file.read().splitlines()
for line in lines:
result = re.search(r"x=(.+)\.\.(.+),y=(.+)\.\.(.+),z=(.+)\.\.(.+)", line).groups()
3 years ago
sign = 1 if line.startswith("on") else -1
result = tuple((sign,)) + tuple(result)
instructions.append(tuple(int(r) for r in result))
return instructions
def volume(cube):
3 years ago
sign, x1, x2, y1, y2, z1, z2 = cube
return (x2 - x1 + 1) * (y2 - y1 + 1) * (z2 - z1 + 1) * sign
3 years ago
def intersect(a, b):
_, ax1, ax2, ay1, ay2, az1, az2 = a
bSign, bx1, bx2, by1, by2, bz1, bz2 = b
if ax1 > bx2 or ax2 < bx1 or ay1 > by2 or ay2 < by1 or az1 > bz2 or az2 < bz1:
3 years ago
return None
3 years ago
x1 = max(ax1, bx1)
x2 = min(ax2, bx2)
y1 = max(ay1, by1)
y2 = min(ay2, by2)
z1 = max(az1, bz1)
z2 = min(az2, bz2)
3 years ago
sign = -bSign
3 years ago
return sign, x1, x2, y1, y2, z1, z2
def reboot():
instructions = readInstructions("input")
cubes = []
3 years ago
for currentCube in instructions:
3 years ago
for cube in cubes.copy():
if intersection := intersect(currentCube, cube):
cubes.append(intersection)
3 years ago
if currentCube[0] == 1:
cubes.append(currentCube)
3 years ago
filteredCubes = filter(lambda c: -50 <= c[1] <= 50, cubes)
print(sum(map(lambda c: volume(c), filteredCubes)))
print(sum(map(lambda c: volume(c), cubes)))
reboot()