def load_image(): def _bit(v): if v == ".": return "0" else: return "1" return Grid.from_file("day20.image", _bit)
def main(): # Part 1 grid = Grid.from_file("day18.input") for _ in range(100): copy = grid.copy() for y in range(grid.height): for x in range(grid.width): p = Position(x, y) neighbours = [n for n in copy.neighbours(p) if copy.get(n) == '#'] match grid.get(p): case '#': if len(neighbours) != 2 and len(neighbours) != 3: grid.set(p, '.') case '.': if len(neighbours) == 3: grid.set(p, '#') print("Part one:", grid.count('#')) # Part 2 grid = Grid.from_file("day18.input") stuck = [Position(0, 0), Position(99, 0), Position(0, 99), Position(99, 99)] for p in stuck: grid.set(p, '#') for _ in range(100): copy = grid.copy() for y in range(grid.height): for x in range(grid.width): p = Position(x, y) neighbours = [n for n in copy.neighbours(p) if copy.get(n) == '#'] match grid.get(p): case '#': if len(neighbours) != 2 and len(neighbours) != 3: if p not in stuck: grid.set(p, '.') case '.': if len(neighbours) == 3: grid.set(p, '#') print("Part two:", grid.count('#'))
def part2(): grid = Grid.from_file("day9.input", value_fn=int) sizes = sorted([len(basin_points(grid, p)) for p in low_points(grid)], reverse=True) return sizes[0] * sizes[1] * sizes[2]
def part1(): grid = Grid.from_file("day9.input", value_fn=int) return sum(grid.get(p) + 1 for p in low_points(grid))
def part2(): grid = Grid.from_file("day11.input", int) for n in range(1_000_000): if step(grid) == (grid.width * grid.height): return n + 1
def part1(): grid = Grid.from_file("day11.input", int) return sum(step(grid) for _ in range(100))