Esempio n. 1
0
File: day20.py Progetto: st3fan/aoc
def part(steps):
    img = load_image()
    alg = load_algorithm()

    max_width = img.width + (steps*2)
    max_height = img.height + (steps*2)
    src = Grid(max_width, max_height, "0")

    for y in range(img.height):
        for x in range(img.width):
            imgp = Position(x, y)
            srcp = Position(x+(max_width-img.width)//2, y+(max_height-img.height)//2)
            src.set(srcp, img.get(imgp))
            
    for step in range(steps):
        dst = Grid(max_width, max_height, "0")
        for y in range(src.height):
            for x in range(src.width):
                p = Position(x, y)
                dst.set(p, lookup(src, p, alg, step))
        src = dst

    return src.count("1")
Esempio n. 2
0
def step(grid: Grid):
    for y in range(grid.height):
        for x in range(grid.width):
            p = Position(x, y)
            grid.set(p, grid.get(p) + 1)

    flashed = set()

    while True:
        c = len(flashed)
        for y in range(grid.height):
            for x in range(grid.width):
                p = Position(x, y)
                if grid.get(p) > 9:
                    if p not in flashed:
                        flash(grid, p, flashed)
        if len(flashed) == c:
            break

    for p in flashed:
        grid.set(p, 0)

    return len(flashed)
Esempio n. 3
0
def main():
    
    instructions = [Instruction.from_string(line.strip()) for line in open("day06.input").readlines()]

    # Part 1

    grid = Grid(1000, 1000, False)

    for i in instructions:
        for y in range(i.fr.y, i.to.y+1):
            for x in range(i.fr.x, i.to.x+1):
                match i.op:
                    case "toggle":
                        grid.set(Position(x, y), not grid.get(Position(x, y)))
                    case "on":
                        grid.set(Position(x, y), True)
                    case "off":
                        grid.set(Position(x, y), False)

    print("Part one:", grid.count(True))

    # Part 2

    grid = Grid(1000, 1000, 0)

    for i in instructions:
        for y in range(i.fr.y, i.to.y+1):
            for x in range(i.fr.x, i.to.x+1):
                match i.op:
                    case "toggle":
                        grid.set(Position(x, y), grid.get(Position(x, y)) + 2)
                    case "on":
                        grid.set(Position(x, y), grid.get(Position(x, y)) + 1)
                    case "off":
                        grid.set(Position(x, y), max(0, grid.get(Position(x, y)) - 1))

    print("Part two:", sum(node for node in grid.nodes))
Esempio n. 4
0
def flash(grid: Grid, p: Position, flashed):
    flashed.add(p)
    for n in grid.neighbours(p, diagonal=True):
        grid.set(n, grid.get(n) + 1)