Skip to content

Commit

Permalink
more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
dcastil committed Dec 16, 2023
1 parent bdbcf3e commit 88ba4a6
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions src/bin/10.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,24 +89,16 @@ impl Grid<'_> {
let next_coordinate = position.get_next_coordinate_unchecked();

self.get_char(next_coordinate).map_or(false, |&character| {
if character == b'.' {
false
} else {
Direction::get_from_pipe_char(character).contains(&position.direction.get_inverse())
}
position.direction.is_connected(character)
})
}

fn get_next_position(&self, position: &Position) -> Position {
let next_coordinate = position.get_next_coordinate_unchecked();

let inverse_direction = position.direction.get_inverse();

let next_direction =
Direction::get_from_pipe_char(*self.get_char_unchecked(&next_coordinate))
.into_iter()
.find(|direction| *direction != inverse_direction)
.unwrap();
let next_direction = position
.direction
.get_next(*self.get_char_unchecked(&next_coordinate))
.unwrap();

Position::new(next_coordinate, next_direction)
}
Expand Down Expand Up @@ -189,6 +181,22 @@ impl Direction {
}
}

fn is_connected(&self, character: u8) -> bool {
if character == b'.' {
false
} else {
Direction::get_from_pipe_char(character).contains(&self.get_inverse())
}
}

fn get_next(&self, character: u8) -> Option<Direction> {
let inverse_direction = self.get_inverse();

Direction::get_from_pipe_char(character)
.into_iter()
.find(|direction| *direction != inverse_direction)
}

fn get_inverse(&self) -> Direction {
match self {
Direction::Up => Direction::Down,
Expand Down

0 comments on commit 88ba4a6

Please sign in to comment.