Day 9: Movie Theater

Megathread guidelines

FAQ

  • CameronDevOPM
    link
    fedilink
    arrow-up
    2
    ·
    edit-2
    3 months ago

    Rust

    pt2: 71ms.

    Wow. What a step up in difficulty. Using the geo library got me the right answer, and quickly in terms of code, but run time was terrible (2m+). Switching back to simply checking if a wall is entirely outside the rectangle got me a much faster win, and the code isn’t too bad either.

    Code and algorithm description

    (A wall is outside if its entirely to the left OR entirely to the right of the rect) OR (entirely above OR entirely below).

       fn check_wall_outside(
            c: &(&Coord, &Coord),
            top: usize,
            bottom: usize,
            left: usize,
            right: usize,
        ) -> bool {
            if (c.0.x <= left && c.1.x <= left) || (c.0.x >= right && c.1.x >= right) {
                return true;
            }
            if (c.0.y <= top && c.1.y <= top) || (c.0.y >= bottom && c.1.y >= bottom) {
                return true;
            }
            false
        }
    
       #[test]
        fn test_y2025_day9_part2_mine1() {
            let input = include_str!("../../input/2025/day_9.txt");
            let coords = input
                .lines()
                .map(|l| {
                    let halfs = l.split_once(',').unwrap();
                    Coord {
                        x: halfs.0.parse::<usize>().unwrap(),
                        y: halfs.1.parse::<usize>().unwrap(),
                    }
                })
                .collect::<Vec<Coord>>();
    
            let mut walls = vec![];
    
            for i in 0..coords.len() {
                let first = &coords[i];
                let second = coords.get(i + 1).unwrap_or(coords.first().unwrap());
    
                walls.push((first, second));
            }
    
            let mut max_area = 0;
            for i in 0..coords.len() {
                let first = &coords[i];
                'next_rect: for j in i..coords.len() {
                    let second = &coords[j];
                    if first == second {
                        continue 'next_rect;
                    }
                    let area = (first.x.abs_diff(second.x) + 1) * (first.y.abs_diff(second.y) + 1);
                    if area < max_area {
                        continue 'next_rect;
                    }
    
                    let (top, bottom) = if first.y > second.y {
                        (second.y, first.y)
                    } else {
                        (first.y, second.y)
                    };
    
                    let (left, right) = if first.x > second.x {
                        (second.x, first.x)
                    } else {
                        (first.x, second.x)
                    };
    
                    for wall in &walls {
                        if !check_wall_outside(wall, top, bottom, left, right) {
                            continue 'next_rect;
                        }
                    }
    
                    max_area = area;
                }
            }
            assert_eq!(max_area, 1542119040);
            println!("Part 2: {}", max_area);
        }