22
submitted 8 months ago* (last edited 8 months ago) by Ategon@programming.dev to c/advent_of_code@programming.dev

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

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

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!()
    }
}
[-] mykl@lemmy.world 1 points 8 months ago

If you're still stuck on part 2, have a look at my comment which shows an unreasonably easy approach :-)

this post was submitted on 10 Dec 2023
22 points (95.8% liked)

Advent Of Code

736 readers
1 users here now

An unofficial home for the advent of code community on programming.dev!

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.

AoC 2023

Solution Threads

M T W T F S S
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25

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 1 year ago
MODERATORS