Skip to content

Instantly share code, notes, and snippets.

@Mu-adventofcode
Last active July 3, 2022 09:25
Show Gist options
  • Save Mu-adventofcode/aeddea772008a07cb70fc6f515c7dae2 to your computer and use it in GitHub Desktop.
Save Mu-adventofcode/aeddea772008a07cb70fc6f515c7dae2 to your computer and use it in GitHub Desktop.
Advent of Code 2021 day 22 part 1
import re
import itertools
LINE_PATTERN = re.compile(
r"(\w+) x=(-?\d+)\.\.(-?\d+),y=(-?\d+)\.\.(-?\d+),z=(-?\d+)\.\.(-?\d+)"
)
def str2r(s1, s2):
start, end = int(s1), int(s2)
if start > 50 or end < -50:
return range(0)
return range(max(start, -50), min(end, 50) + 1)
data = open("input_22.txt").read().strip()
steps = (
(f[0] == "on", str2r(f[1], f[2]), str2r(f[3], f[4]), str2r(f[5], f[6]))
for f in re.findall(LINE_PATTERN, data)
)
grid = {}
for b, rx, ry, rz in steps:
for cube in itertools.product(rx, ry, rz):
grid[cube] = b
print(sum(grid.values()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment