|
| 1 | +import logging |
| 2 | + |
| 3 | +logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG) |
| 4 | +logging.disable(logging.FATAL) |
| 5 | + |
| 6 | +def load_cubes(filename): |
| 7 | + active_cubes = set() |
| 8 | + lines = [] |
| 9 | + with open(filename, "r") as f: |
| 10 | + for line in f: |
| 11 | + lines.append(line.rstrip()) |
| 12 | + z = 0 |
| 13 | + for y, row in enumerate(lines): |
| 14 | + for x, col in enumerate(row): |
| 15 | + if col == "#": |
| 16 | + active_cubes.add((x, y, z)) |
| 17 | + return active_cubes |
| 18 | + |
| 19 | +# there is no (0, 0, 0) here :) |
| 20 | +relative_neighbours = ((-1, -1, -1), (-1, -1, 0), (-1, -1, 1), (-1, 0, -1), |
| 21 | + (-1, 0, 0), (-1, 0, 1), (-1, 1, -1), (-1, 1, 0), |
| 22 | + (-1, 1, 1), ( 0, -1, -1), ( 0, -1, 0), ( 0, -1, 1), |
| 23 | + ( 0, 0, -1), ( 0, 0, 1), ( 0, 1, -1), ( 0, 1, 0), |
| 24 | + ( 0, 1, 1), ( 1, -1, -1), ( 1, -1, 0), ( 1, -1, 1), |
| 25 | + ( 1, 0, -1), ( 1, 0, 0), ( 1, 0, 1), ( 1, 1, -1), |
| 26 | + ( 1, 1, 0), ( 1, 1, 1)) |
| 27 | + |
| 28 | +def get_neighbours(point): |
| 29 | + logging.debug(point) |
| 30 | + return tuple(map(lambda rel : (rel[0] + point[0], |
| 31 | + rel[1] + point[1], |
| 32 | + rel[2] + point[2]), |
| 33 | + relative_neighbours)) |
| 34 | + |
| 35 | +# only neighbours and currently active guys are of intrest |
| 36 | +def get_points_of_intrest(active_cubes): |
| 37 | + points_of_intrest = active_cubes.copy() |
| 38 | + logging.debug("old active cubes: %s", points_of_intrest) |
| 39 | + neighs_set = set() |
| 40 | + for point in points_of_intrest: |
| 41 | + neighs_set.update(get_neighbours(point)) |
| 42 | + logging.debug("new neighbours: %s", neighs_set) |
| 43 | + points_of_intrest.update(neighs_set) |
| 44 | + logging.debug("final points of intrest: %s" % points_of_intrest) |
| 45 | + return points_of_intrest |
| 46 | + |
| 47 | +def count_neighbours(points_of_intrest, active_cubes): |
| 48 | + neighbour_counts = {} |
| 49 | + logging.debug("count neighbours POIs: %s" % points_of_intrest) |
| 50 | + for point in points_of_intrest: |
| 51 | + act_neighs = sum(map(lambda x: x in active_cubes, get_neighbours(point))) |
| 52 | + neighbour_counts[point] = act_neighs |
| 53 | + return neighbour_counts |
| 54 | + |
| 55 | +def new_cubes(points_of_intrest, active_cubes, neighbour_counts): |
| 56 | + new_active = set() |
| 57 | + logging.debug("new cubes POIs: %s" % points_of_intrest) |
| 58 | + for point in points_of_intrest: |
| 59 | + if point in active_cubes: |
| 60 | + if neighbour_counts[point] in (2, 3): |
| 61 | + new_active.add(point) |
| 62 | + else: |
| 63 | + if neighbour_counts[point] == 3: |
| 64 | + new_active.add(point) |
| 65 | + return new_active |
| 66 | + |
| 67 | + |
| 68 | +active_cubes = load_cubes("test_input") |
| 69 | +logging.debug("active cubes: %s" % active_cubes) |
| 70 | + |
| 71 | +for i in range(6): |
| 72 | + points_of_intrest = get_points_of_intrest(active_cubes) |
| 73 | + neighbour_counts = count_neighbours(points_of_intrest, active_cubes) |
| 74 | + active_cubes = new_cubes(points_of_intrest, active_cubes, neighbour_counts) |
| 75 | + |
| 76 | +print(len(active_cubes)) |
| 77 | + |
| 78 | + |
| 79 | + |
0 commit comments