AoC 2026 Day 3: So many squares

Source: Day 3: Foggy Forest

Full solution for today (spoilers!).

Part 1

Given a grid of empty (.) and full (#) count how many squares have all four points at a #.

Well that’s fun. The main issue here is to avoid overcounting. So I’m going to do this the direct way:

  • Load all of the points
  • Loop over all ‘first’ points
  • For each: Loop over all ‘second’ points, but only consider those directly below the original points (the y / .1 is equal and the x must be strictly greater)
  • For each pair: Calculate and check the other two points those would define; count if they’re both present
#[aoc::register]
fn part1(input: &str) -> impl Into<String> {
    let mut points: FxHashSet<(usize, usize)> = FxHashSet::default();

    for (y, line) in input.lines().enumerate() {
        for (x, c) in line.chars().enumerate() {
            if c == '#' {
                points.insert((x, y));
            }
        }
    }

    let mut rects = 0;

    for p1 in points.iter() {
        for p2 in points.iter() {
            if p1 == p2 || p2.0 <= p1.0 || p2.1 != p1.1 {
                continue;
            }

            let size = p2.0 - p1.0;

            let p3 = (p1.0, p1.1 + size);
            let p4 = (p1.0 + size, p1.1 + size);

            if points.contains(&p3) && points.contains(&p4) {
                rects += 1;
            }
        }
    }

    rects.to_string()
}

And here we go:

$ just run 3

part1: 352

$ just bench 3

part1: 4.365291ms ± 201.419µs [min: 4.010417ms, max: 4.99275ms, median: 4.352208ms]

I could probably make that faster–I expect storing it all as a vec rather than a HashSet would be faster, since it’s small enough. But really, I spent more time on visualizations, so… It’s fast enough for now!

Part 2

Do the same but allow diagonal lines.

This is basically the same thing, although we have to:

  • Loosen the criteria that throw out points. This time include all points that have an x that is less than or equal to and a y that is strictly less. That way we won’t include any points twice
  • p3 and p4 add the x difference to the y and the y to the x to generate the other two points
  • We do have to make sure that the x of p3 doesn’t go below 0; those would be invalid and would underflow the usize, so special case that
#[aoc::register]
fn part2(input: &str) -> impl Into<String> {
    let mut points: FxHashSet<(usize, usize)> = FxHashSet::default();

    for (y, line) in input.lines().enumerate() {
        for (x, c) in line.chars().enumerate() {
            if c == '#' {
                points.insert((x, y));
            }
        }
    }

    let mut rects = 0;

    for p1 in points.iter() {
        for p2 in points.iter() {
            if p1 == p2 || p2.0 <= p1.0 || p2.1 < p1.1 {
                continue;
            }

            let dx = p2.0 - p1.0;
            let dy = p2.1 - p1.1;

            if p1.0 < dy {
                continue;
            }

            let p3 = (p1.0 - dy, p1.1 + dx);
            let p4 = (p3.0 + dx, p3.1 + dy);

            if points.contains(&p3) && points.contains(&p4) {
                rects += 1;
            }
        }
    }

    rects.to_string()
}

Similar to part 1:

$ just run 3

part2: 1794

$ just bench 3

part2: 11.272115ms ± 232.217µs [min: 10.859958ms, max: 11.976084ms, median: 11.2435ms]

A bit slower… because it’s doing a lot more work. Still well within $$O(n^2)$$. Not sure how you could even do it algorithmically faster than that, so it’s all in the constants.

Visualization

Part 1:

Part 2:

Benchmarks

$ just bench 3

part1: 4.365291ms ± 201.419µs [min: 4.010417ms, max: 4.99275ms, median: 4.352208ms]
part2: 11.272115ms ± 232.217µs [min: 10.859958ms, max: 11.976084ms, median: 11.2435ms]