23

Day 3: Foggy Forest

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

you are viewing a single comment's thread
view the rest of the comments
[-] shape_warrior_t@programming.dev 1 points 20 hours ago

Rust

Spent most of the time writing a 2D grid abstraction and writing up the solution, rather than actually solving the problem. In fact, had the problem solved before Day 4 came out, and just took until now to figure out a reasonable way to explain it in my writeup.

Basically: do a double for loop through the set of all trees. For part 1, the two trees should be the top-left and bottom-right corners of the square (check to make sure they're properly diagonal from each other, and that the other two corners have trees). For part 2, the two trees should be consecutive corners of the square (find the other two corners by continuing the walk around the square, and make sure they have trees). In part 2, we need to divide by 4 to account for duplicates (each square is counted once per side). Solution completes "instantly" in release mode, so I'm pretty satisfied with the performance, at least compared to a lot of other people in this thread.

Relevant parts of code

pub fn part_1(input: &str) -> Num {
    let map: Grid<Tile> = input.parse().unwrap();
    let trees: Vec<_> = map
        .coordinates()
        .filter(|&index| map[index] == Tile::Tree)
        .collect();
    iproduct!(&trees, &trees)
        .filter(|&(&(x0, y0), &(x1, y1))| {
            x1 - x0 == y1 - y0
                && x0 < x1
                && y0 < y1
                && map[(x1, y0)] == Tile::Tree
                && map[(x0, y1)] == Tile::Tree
        })
        .count() as _
}

pub fn part_2(input: &str) -> Num {
    let map: Grid<Tile> = input.parse().unwrap();
    let trees: Vec<_> = map
        .coordinates()
        .filter(|&index| map[index] == Tile::Tree)
        .collect();
    let rooted_squares = iproduct!(&trees, &trees)
        .filter_map(|(&t0, &t1)| {
            if t0 == t1 {
                return None;
            }
            let d0 = sub(t1, t0);
            assert_eq!(t1, add(t0, d0));
            let d1 = rotate(d0);
            let t2 = add(t1, d1);
            let d2 = rotate(d1);
            let t3 = add(t2, d2);
            let d3 = rotate(d2);
            assert_eq!(t0, add(t3, d3));
            assert_eq!(d0, rotate(d3));
            (map.get(t2) == Some(&Tile::Tree) && map.get(t3) == Some(&Tile::Tree))
                .then_some((t0, t1, t2, t3))
        })
        .count();
    assert_eq!(rooted_squares % 4, 0);
    (rooted_squares / 4) as _
}

[-] CameronDev@programming.dev 2 points 15 hours ago

For pt2, another way to handle/prevent duplicates is to remove the trees as you go. Most of my performance issues went away in release mode, i got pt2 to 2-3ms, which is good enough. Would prefer <1ms, but further optimising is basically just taking someone elses solution, so I'll live with my inadequacy :D

this post was submitted on 03 Dec 2026
23 points (100.0% liked)

Advent Of Code

1278 readers
56 users here now

An unofficial home for the advent of code community on programming.dev! Other challenges are also welcome!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

Everybody Codes is another collection of programming puzzles with seasonal events.

EC 2025

AoC 2026

Solution Threads

M T W T F S S
1 2 3 4 5 6
7 8 9 10 11 12

Visualisations Megathread

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 3 years ago
MODERATORS