Skip to content
advent of code 2023

day 18

lavaduct lagoon

approach

the dig plan is turned into the list of corner points by walking each instruction in one jump. the area comes from the shoelace formula (signed_area) and the perimeter from summing the lengths of the edges. the number of dug cells is perimeter / 2 + area + 1, which is pick's theorem plus the boundary.

part 2 reads the direction and distance from the hex colour instead and runs the same formula, so the huge distances cost nothing. leftover helpers from an earlier attempt (walking the whole border, a flood fill) are still in the file but unused.

the trick

shoelace area plus half the perimeter plus one counts every dug cell without drawing the trench.

python
def signed_area(polygon):
    res = det(polygon[0], polygon[-1])
    for p, q in zip(polygon, polygon[1:]):
        res += det(p, q)
    return res / 2
...
def part2(plan):
    poly = list(walk_p2(plan))
    p = perimeter(poly)
    a = abs(signed_area(poly))
    return int(p // 2 + a) + 1