Last active
July 3, 2022 09:25
-
-
Save Mu-adventofcode/aeddea772008a07cb70fc6f515c7dae2 to your computer and use it in GitHub Desktop.
Advent of Code 2021 day 22 part 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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