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

  • shape_warrior_t@programming.dev
    link
    fedilink
    arrow-up
    1
    ·
    1 day 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.devOPM
      link
      fedilink
      arrow-up
      2
      ·
      22 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