diff --git a/day12/common.rs b/day12/common.rs index c6ef906..c47b257 100644 --- a/day12/common.rs +++ b/day12/common.rs @@ -89,7 +89,7 @@ impl Map { self.end_pos } - fn neighbors(&self, pos: (usize, usize)) -> Option> { + pub fn neighbors(&self, pos: (usize, usize)) -> Option> { let mut result = vec![]; let height = self.square_height(pos)?; let (x, y) = pos; @@ -133,10 +133,13 @@ impl Map { result } - pub fn shortest_path(&self) -> ShortestPath { + pub fn shortest_path(&self, start_pos: Option<(usize, usize)>) -> ShortestPath { // we reverse start_pos and end_pos so the ShortestPath will have the right order + let end_pos = match start_pos { + Some(pos) => pos, + None => self.start_pos, + }; let start_pos = self.end_pos; - let end_pos = self.start_pos; let mut table = self.dijkstra_table(start_pos); let mut unvisited: HashSet<(usize, usize)> = table.iter().map(|i| i.pos).collect(); @@ -163,6 +166,18 @@ impl Map { start_pos: start_pos, } } + + pub fn lowest_positions(&self) -> Vec<(usize, usize)> { + let mut result = vec![]; + for y in 0..self.squares.len() { + for x in 0..self.squares[0].len() { + if self.squares[y][x] as char == 'a' { + result.push((x, y)); + } + } + } + result + } } impl Iterator for ShortestPath { @@ -178,7 +193,7 @@ impl Iterator for ShortestPath { .iter() .find(|i| i.pos == self.current) .unwrap(); - self.current = info.previous.unwrap(); + self.current = info.previous?; Some(result) } } diff --git a/day12/part1.rs b/day12/part1.rs index 97b979e..028fd32 100644 --- a/day12/part1.rs +++ b/day12/part1.rs @@ -21,6 +21,6 @@ use common::Map; fn main() -> Result<(), &'static str> { let map = Map::from_stdin()?; - println!("{:?}", map.shortest_path().count()); + println!("{:?}", map.shortest_path(None).count()); Ok(()) }