Skip Navigation

❄️ - 2023 DAY 10 SOLUTIONS -❄️

Day 10: Pipe Maze

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)
  • Code block support is not fully rolled out yet but likely will be in the middle of the event. Try to share solutions as both code blocks and using something such as https://topaz.github.io/paste/ , pastebin, or github (code blocks to future proof it for when 0.19 comes out and since code blocks currently function in some apps and some instances as well if they are running a 0.19 beta)

FAQ


🔒 Thread is locked until there's at least 100 2 star entries on the global leaderboard

🔓 Unlocked after 40 mins

27

You're viewing a single thread.

27 comments
  • Well, star one is solved. I don't love the code, but yet again, it works for now. I don't love the use of a label to continue/break a loop, and the valid_steps function is a mess that could probably be done much cleaner.

    Upon looking at star 2 I don't even have the slightest idea of where to start. I may have to come back to this one at a later date. Sigh.

    https://github.com/capitalpb/advent_of_code_2023/blob/main/src/solvers/day10.rs

    use crate::Solver;
    
    #[derive(Debug)]
    struct PipeMap {
        start: usize,
        tiles: Vec,
        width: usize,
    }
    
    impl PipeMap {
        fn from(input: &str) -> PipeMap {
            let tiles = input
                .lines()
                .rev()
                .flat_map(|row| row.chars())
                .collect::>();
    
            let width = input.find('\n').unwrap();
            let start = tiles.iter().position(|tile| tile == &'S').unwrap();
    
            PipeMap {
                start,
                tiles,
                width,
            }
        }
    
        fn valid_steps(&self, index: usize) -> Vec {
            let mut tiles = vec![];
            let current_tile = *self.tiles.get(index).unwrap();
    
            if "S|LJ".contains(current_tile) {
                let north = index + self.width;
                if let Some(tile) = self.tiles.get(north) {
                    if "|7F".contains(*tile) {
                        tiles.push(north);
                    }
                }
            }
    
            if "S|7F".contains(current_tile) {
                if let Some(south) = index.checked_sub(self.width) {
                    if let Some(tile) = self.tiles.get(south) {
                        if "|LJ".contains(*tile) {
                            tiles.push(south);
                        }
                    }
                }
            }
    
            if "S-J7".contains(current_tile) {
                if let Some(west) = index.checked_sub(1) {
                    if (west % self.width) != (self.width - 1) {
                        if let Some(tile) = self.tiles.get(west) {
                            if "-LF".contains(*tile) {
                                tiles.push(west);
                            }
                        }
                    }
                }
            }
    
            if "S-LF".contains(current_tile) {
                let east = index + 1;
                if east % self.width != 0 {
                    if let Some(tile) = self.tiles.get(east) {
                        if "-J7".contains(*tile) {
                            tiles.push(east);
                        }
                    }
                }
            }
    
            tiles
        }
    }
    
    pub struct Day10;
    
    impl Solver for Day10 {
        fn star_one(&self, input: &str) -> String {
            let pipe_map = PipeMap::from(input);
    
            let mut current_pos = pipe_map.start;
            let mut last_pos = pipe_map.start;
            let mut steps: usize = 0;
    
            'outer: loop {
                for pos in pipe_map.valid_steps(current_pos) {
                    if pos != last_pos {
                        last_pos = current_pos;
                        current_pos = pos;
                        steps += 1;
    
                        continue 'outer;
                    }
                }
                break;
            }
    
            steps.div_ceil(2).to_string()
        }
    
        fn star_two(&self, input: &str) -> String {
            todo!()
        }
    }
    
    • If you're still stuck on part 2, have a look at my comment which shows an unreasonably easy approach :-)

You've viewed 27 comments.