Beispiel #1
0
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")
Beispiel #2
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))