Skip to content

Commit

Permalink
day10: Support different starting piece
Browse files Browse the repository at this point in the history
  • Loading branch information
pedantic79 committed Jan 3, 2024
1 parent 87fe112 commit c470b0f
Showing 1 changed file with 36 additions and 10 deletions.
46 changes: 36 additions & 10 deletions src/day10.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,32 @@ fn next_direction(pipe: u8, dir: &Dir) -> Option<Dir> {
})
}

fn get(grid: &[Vec<u8>], r: usize, c: usize) -> &u8 {
grid.get(r).and_then(|x| x.get(c)).unwrap_or(&b'.')
}

fn determine_start(grid: &[Vec<u8>], r: usize, c: usize) -> (u8, Dir) {
let up = b"|7F".contains(get(grid, r.wrapping_sub(1), c));
let down = b"|LJ".contains(get(grid, r + 1, c));
let left = b"-FL".contains(get(grid, r, c.wrapping_sub(1)));
let right = b"-J7".contains(get(grid, r, c + 1));

match (up, down, left, right) {
(false, false, true, true) => (b'-', Dir::Right),
(false, true, false, true) => (b'F', Dir::Down),
(false, true, true, false) => (b'7', Dir::Down),
(true, false, false, true) => (b'L', Dir::Right),
(true, false, true, false) => (b'J', Dir::Up),
(true, true, false, false) => (b'|', Dir::Down),
_ => panic!("unknown piece {up} {down} {left} {right}"),
}
}

#[aoc_generator(day10)]
pub fn generator(input: &str) -> (HashSet<(usize, usize)>, Vec<Vec<u8>>) {
let mut start = (0, 0);
let mut start: (usize, usize) = (0, 0);

let grid: Vec<_> = input
let mut grid: Vec<Vec<u8>> = input
.lines()
.enumerate()
.map(|(row, line)| {
Expand All @@ -59,25 +80,30 @@ pub fn generator(input: &str) -> (HashSet<(usize, usize)>, Vec<Vec<u8>>) {
})
.collect();

(solve(&grid, start), grid)
let (start_pipe, start_dir) = determine_start(&grid, start.0, start.1);
grid[start.0][start.1] = start_pipe;

(solve(&grid, start, start_dir), grid)
}

fn solve(grid: &[Vec<u8>], start: (usize, usize)) -> HashSet<(usize, usize)> {
fn solve(grid: &[Vec<u8>], start: (usize, usize), mut dir: Dir) -> HashSet<(usize, usize)> {
let (mut r, mut c) = start;
let mut dir = Dir::Down; // assume we can go down first
let mut pipe_set = HashSet::new();
pipe_set.insert((r, c));

loop {
dir.next_pos(&mut r, &mut c);
let pipe = grid[r][c];
if pipe == b'S' {
if (r, c) == start {
break;
}
pipe_set.insert((r, c));

dir = next_direction(pipe, &dir)
.unwrap_or_else(|| panic!("Unknown pipe combination {} {:?}", char::from(pipe), dir,));
dir = next_direction(grid[r][c], &dir).unwrap_or_else(|| {
panic!(
"Unknown pipe combination {} {:?}",
char::from(grid[r][c]),
dir,
)
});
}

pipe_set
Expand Down

0 comments on commit c470b0f

Please sign in to comment.