@@ -82,7 +82,7 @@ fn get_neighbors(map: &Vec<Vec<i32>>, pos: Pos) -> Vec<Pos> {
8282 } ) . filter_map ( |e| e) . collect ( )
8383}
8484
85- fn part1 ( map : & Vec < Vec < i32 > > , start : Pos , end : Pos ) {
85+ fn compute_cost ( map : & Vec < Vec < i32 > > , start : Pos ) -> HashMap < Pos , Cost > {
8686 // A simplified version of Dijkstra.
8787 let mut processed: HashMap < Pos , Cost > = HashMap :: new ( ) ;
8888 let mut to_process: VecDeque < Processable > = VecDeque :: new ( ) ;
@@ -103,9 +103,34 @@ fn part1(map: &Vec<Vec<i32>>, start: Pos, end: Pos) {
103103 to_process. push_back ( Processable :: new ( neighbor, new_cost) ) ;
104104 }
105105 }
106+ return processed;
107+ }
108+
109+ fn part1 ( map : & Vec < Vec < i32 > > , start : Pos , end : Pos ) {
110+ let processed = compute_cost ( & map, start) ;
106111 println ! ( "Part 1: {}" , processed. get( & end) . unwrap( ) ) ;
107112}
108113
114+ fn part2 ( map : & Vec < Vec < i32 > > , end : Pos ) {
115+ // Let's cheese this solution be observing that the only 'b' steps are
116+ // on the left hand side of the map. Therefore one can only get up
117+ // the hill from the left-most positions.
118+ let mut lowest_cost: usize = std:: usize:: MAX ;
119+ for y in 0 ..map. len ( ) {
120+ let start = Pos { x : 0 , y : y as i32 } ;
121+ let processed = compute_cost ( map, start) ;
122+ // println!("best 1: {}", y);
123+ let cost = * processed. get ( & end) . unwrap ( ) ;
124+ // println!("best 1: {}", cost);
125+ if cost < lowest_cost {
126+ // println!("best 2: {}", y);
127+ lowest_cost = cost;
128+ }
129+ }
130+
131+ println ! ( "Part 2: {}" , lowest_cost) ;
132+ }
133+
109134pub fn run ( ) {
110135 let input = input_reader:: read_file_in_cwd ( "src/day_12.data" ) ;
111136
@@ -139,6 +164,7 @@ pub fn run() {
139164
140165 let start = Pos { x : start_x, y : start_y } ;
141166 let end = Pos { x : end_x, y : end_y } ;
142-
167+
143168 part1 ( & map, start, end) ;
169+ part2 ( & map, end) ;
144170}
0 commit comments