Skip to content

Instantly share code, notes, and snippets.

@thrasibule
Created January 24, 2022 03:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thrasibule/a6325cead7f0fe5f9f3897a681655ffc to your computer and use it in GitHub Desktop.
Save thrasibule/a6325cead7f0fe5f9f3897a681655ffc to your computer and use it in GitHub Desktop.
import numpy as np
import re
fh = open("input22")
cubes = []
X, Y, Z = set(), set(), set()
for i, line in enumerate(fh):
line = line.strip()
m = re.match("(on|off) x=([^.]*)..([^,]*),y=([^.]*)..([^,]*),z=([^.]*)..(.*)", line)
action, *coords = m.groups()
arr = np.array([int(c) for c in coords])
arr += np.array([0, 1, 0, 1, 0, 1])
g = iter(arr)
x, y, z = zip(g, g)
X |= set(x)
Y |= set(y)
Z |= set(z)
cubes.append((x, y, z, action == "on"))
X = sorted(X)
Y = sorted(Y)
Z = sorted(Z)
volumes = np.einsum("i,j,k", np.diff(X), np.diff(Y), np.diff(Z))
Xdict = {x: i for i, x in enumerate(X)}
Ydict = {y: i for i, y in enumerate(Y)}
Zdict = {z: i for i, z in enumerate(Z)}
area = np.zeros((len(X) - 1, len(Y) - 1, len(Z) - 1), "bool")
for x, y, z, action in cubes:
area[Xdict[x[0]]:Xdict[x[1]], Ydict[y[0]]:Ydict[y[1]], Zdict[z[0]]:Zdict[z[1]]] = action
print(np.sum(volumes[area]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment