#!/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)