-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
178 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
use std::cmp::Reverse; | ||
use std::collections::{BinaryHeap, HashSet}; | ||
|
||
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] | ||
enum Direction { | ||
U, | ||
L, | ||
D, | ||
R, | ||
} | ||
|
||
impl Direction { | ||
fn next(&self) -> Direction { | ||
match self { | ||
Direction::U => Direction::L, | ||
Direction::L => Direction::D, | ||
Direction::D => Direction::R, | ||
Direction::R => Direction::U, | ||
} | ||
} | ||
fn prev(&self) -> Direction { | ||
match self { | ||
Direction::U => Direction::R, | ||
Direction::L => Direction::U, | ||
Direction::D => Direction::L, | ||
Direction::R => Direction::D, | ||
} | ||
} | ||
} | ||
|
||
fn step(y: usize, x: usize, dir: Direction) -> Option<(usize, usize)> { | ||
Some(match dir { | ||
Direction::U => (y.checked_sub(1)?, x), | ||
Direction::L => (y, x.checked_sub(1)?), | ||
Direction::D => (y.checked_add(1)?, x), | ||
Direction::R => (y, x.checked_add(1)?), | ||
}) | ||
} | ||
|
||
fn solve<P, F, T>(data: &str, ok: P, next: F) -> Option<usize> | ||
where | ||
P: Fn(usize) -> bool, | ||
F: Fn(Direction, usize) -> T, | ||
T: IntoIterator, | ||
<T as IntoIterator>::Item: Into<Direction>, | ||
{ | ||
let maze = data | ||
.lines() | ||
.filter_map(|line| { | ||
line.chars() | ||
.map(|c| c.to_digit(10).filter(|&d| d > 0)) | ||
.collect::<Option<Vec<_>>>() | ||
}) | ||
.collect::<Vec<_>>(); | ||
let mut queue: BinaryHeap<_> = [(Reverse(0), (0, 0, Direction::R, 0))].into(); | ||
let mut visited = HashSet::new(); | ||
while let Some((Reverse(cost), state @ (y, x, direction, distance))) = queue.pop() { | ||
if y == maze.len() - 1 && x == maze[y].len() - 1 && ok(distance) { | ||
return Some(cost); | ||
} | ||
if !visited.insert(state) { | ||
continue; | ||
} | ||
for direction in next(direction, distance) { | ||
let direction = direction.into(); | ||
let Some((y, x)) = | ||
step(y, x, direction).filter(|&(y, x)| y < maze.len() && x < maze[y].len()) | ||
else { | ||
continue; | ||
}; | ||
queue.push(( | ||
Reverse(cost + maze[y][x] as usize), | ||
( | ||
y, | ||
x, | ||
direction, | ||
if direction == state.2 { | ||
distance + 1 | ||
} else { | ||
1 | ||
}, | ||
), | ||
)); | ||
} | ||
} | ||
None | ||
} | ||
|
||
pub fn part1(data: &str) -> Option<usize> { | ||
solve( | ||
data, | ||
|_| true, | ||
|direction, distance| { | ||
if distance < 3 { | ||
vec![direction.prev(), direction.next(), direction] | ||
} else { | ||
vec![direction.prev(), direction.next()] | ||
} | ||
}, | ||
) | ||
} | ||
|
||
pub fn part2(data: &str) -> Option<usize> { | ||
solve( | ||
data, | ||
|distance| distance >= 4, | ||
|direction, distance| { | ||
if distance < 4 { | ||
vec![direction] | ||
} else if distance < 10 { | ||
vec![direction.prev(), direction.next(), direction] | ||
} else { | ||
vec![direction.prev(), direction.next()] | ||
} | ||
}, | ||
) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use indoc::indoc; | ||
use pretty_assertions::assert_eq; | ||
|
||
static EXAMPLE_1: &str = indoc! {" | ||
2413432311323 | ||
3215453535623 | ||
3255245654254 | ||
3446585845452 | ||
4546657867536 | ||
1438598798454 | ||
4457876987766 | ||
3637877979653 | ||
4654967986887 | ||
4564679986453 | ||
1224686865563 | ||
2546548887735 | ||
4322674655533 | ||
"}; | ||
static EXAMPLE_2: &str = indoc! {" | ||
111111111111 | ||
999999999991 | ||
999999999991 | ||
999999999991 | ||
999999999991 | ||
"}; | ||
|
||
#[test] | ||
fn part1_examples() { | ||
assert_eq!(Some(102), part1(EXAMPLE_1)); | ||
} | ||
|
||
#[test] | ||
fn part2_examples() { | ||
assert_eq!(Some(94), part2(EXAMPLE_1)); | ||
assert_eq!(Some(71), part2(EXAMPLE_2)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters