Files
python-aoc-2022/day18/part2.py
Sebastian Seedorf 2ed0ba2108 Day 18
2022-12-23 20:33:11 +01:00

23 lines
636 B
Python

#!/usr/bin/env python3
lines = (x.strip() for x in open("input.txt"))
queue = [(0, 0, 0)]
cnt = 0
visited = set()
cubes = set()
for line in lines:
x, y, z = map(int, line.split(","))
cubes.add((x, y, z))
while queue:
x, y, z = queue.pop(0)
for xn, yn, zn in ((1, 0, 0), (0, 1, 0), (0, 0, 1), (-1, 0, 0), (0, -1, 0), (0, 0, -1)):
neighbor = (x+xn, y+yn, z+zn)
if neighbor in cubes:
cnt += 1
elif neighbor not in visited and -2 <= neighbor[0] < 22 and -2 <= neighbor[1] < 22 and -2 <= neighbor[2] < 22:
queue.append(neighbor)
visited.add(neighbor)
print(cnt)