Skip to content
advent of code 2021

day 13

transparent origami

approach

the dots are kept as a set of (x, y) points. a fold along x = k keeps every point left of the line and mirrors the rest to 2k - x; folding along y is the same on the other axis. because it's a set, points that land on top of each other merge for free.

part 1 is the size of the set after the first fold. part 2 applies every fold and then prints the remaining points as a grid of #, which spells out the answer.

the trick

a fold is just a reflection, x → 2k - x, applied to the points past the line.

python
def fold_x(x, points):
    s = set()
    for px, py in points:
        if px < x:
            s.add((px, py))
        else:
            s.add((2 * x - px, py))
    return s