Skip to content
advent of code 2022

day 22

monkey map

approach

the map is stored as a dict from Point to character, and the path is split into numbers and turn letters with groupby. directions are Points too, with cw and ccw rotations. part 1 wraps by stepping with modulo arithmetic over the bounding box until it lands on a real tile.

part 2 folds the map into a cube without hard-coding the layout. it traces the outer border of the map, records each border cell with its outward normal, and marks the concave corners ("valleys"). from each valley, stitch walks two cursors outwards in opposite directions, pairing the edge cells they pass until both have turned a corner, so the two edges that meet when the cube is folded get glued together. walking off the map then looks up the paired cell and leaves in the opposite of its normal.

sol2.py and sol3.py are earlier attempts that only parse the faces and find border cells, and print debugging output.

the trick

edges that meet at a concave corner of the net are glued together on the cube; zip them outward from each corner until they turn.

python
    for i1, i2 in cursors:
        c1 = Cursor(i1, i1)
        c2 = Cursor(i2, i2)
        while not is_turn(nportals, c1) or not is_turn(nportals, c2):
            d[nportals[c1.i]] = nportals[c2.i]
            d[nportals[c2.i]] = nportals[c1.i]

            c1 = Cursor((c1.i - 1) % n, c1.i % n)
            c2 = Cursor((c2.i + 1) % n, c2.i % n)