Day 24, part 2
This commit is contained in:
		@ -8,6 +8,6 @@ pub fn main() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    day24::print_map(&map, &start);
 | 
					    day24::print_map(&map, &start);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let rounds = day24::find_path(&map, &start, &target);
 | 
					    let (rounds, _) = day24::find_path(&map, &start, &target);
 | 
				
			||||||
    println!("Goal is reachable in {} min", rounds);
 | 
					    println!("Goal is reachable in {} min", rounds);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										19
									
								
								src/bin/d24p2.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								src/bin/d24p2.rs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,19 @@
 | 
				
			|||||||
 | 
					use aoc22::{
 | 
				
			||||||
 | 
					    day24::{self},
 | 
				
			||||||
 | 
					    util,
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub fn main() {
 | 
				
			||||||
 | 
					    let (map, start, target) = day24::parse_map(&util::parse_input());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    day24::print_map(&map, &start);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let (rounds_1, map_1) = day24::find_path(&map, &start, &target);
 | 
				
			||||||
 | 
					    println!("Goal is reachable in {} min", rounds_1);
 | 
				
			||||||
 | 
					    let (rounds_2, map_2) = day24::find_path(&map_1, &target, &start);
 | 
				
			||||||
 | 
					    println!("Start is reachable in {} min", rounds_2);
 | 
				
			||||||
 | 
					    let (rounds_3, _) = day24::find_path(&map_2, &start, &target);
 | 
				
			||||||
 | 
					    println!("Goal is reachable in {} min", rounds_2);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    println!("Total time taken is {} min", rounds_1 + rounds_2 + rounds_3);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										29
									
								
								src/day24.rs
									
									
									
									
									
								
							
							
						
						
									
										29
									
								
								src/day24.rs
									
									
									
									
									
								
							@ -139,7 +139,15 @@ pub fn next_map(map: &Vec<Vec<Tile>>) -> Vec<Vec<Tile>> {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// Returns length of the shortest path
 | 
					/// Returns length of the shortest path
 | 
				
			||||||
pub fn find_path(initial: &Vec<Vec<Tile>>, start: &Coord, target: &Coord) -> usize {
 | 
					pub fn find_path(
 | 
				
			||||||
 | 
					    initial: &Vec<Vec<Tile>>,
 | 
				
			||||||
 | 
					    start: &Coord,
 | 
				
			||||||
 | 
					    target: &Coord,
 | 
				
			||||||
 | 
					) -> (usize, Vec<Vec<Tile>>) {
 | 
				
			||||||
 | 
					    let height = initial.len();
 | 
				
			||||||
 | 
					    assert!(height > 0);
 | 
				
			||||||
 | 
					    let width = initial[0].len();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let mut map = initial.clone();
 | 
					    let mut map = initial.clone();
 | 
				
			||||||
    let mut to_check = HashSet::new();
 | 
					    let mut to_check = HashSet::new();
 | 
				
			||||||
    to_check.insert(*start);
 | 
					    to_check.insert(*start);
 | 
				
			||||||
@ -151,20 +159,23 @@ pub fn find_path(initial: &Vec<Vec<Tile>>, start: &Coord, target: &Coord) -> usi
 | 
				
			|||||||
        let mut check_next = HashSet::new();
 | 
					        let mut check_next = HashSet::new();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        for pos in to_check {
 | 
					        for pos in to_check {
 | 
				
			||||||
            let mut reachable = vec![
 | 
					            let mut reachable = vec![pos];
 | 
				
			||||||
                pos,
 | 
					 | 
				
			||||||
                (pos.0 + 1, pos.1),
 | 
					 | 
				
			||||||
                (pos.0, pos.1 + 1),
 | 
					 | 
				
			||||||
                (pos.0, pos.1 - 1),
 | 
					 | 
				
			||||||
            ];
 | 
					 | 
				
			||||||
            // This would underflow in the first round
 | 
					 | 
				
			||||||
            if pos.0 != 0 {
 | 
					            if pos.0 != 0 {
 | 
				
			||||||
                reachable.push((pos.0 - 1, pos.1));
 | 
					                reachable.push((pos.0 - 1, pos.1));
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					            if pos.0 != height - 1 {
 | 
				
			||||||
 | 
					                reachable.push((pos.0 + 1, pos.1));
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            if pos.1 != 0 {
 | 
				
			||||||
 | 
					                reachable.push((pos.0, pos.1 - 1));
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            if pos.1 != width - 1 {
 | 
				
			||||||
 | 
					                reachable.push((pos.0, pos.1 + 1));
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            for p in reachable {
 | 
					            for p in reachable {
 | 
				
			||||||
                if p == *target {
 | 
					                if p == *target {
 | 
				
			||||||
                    return rounds;
 | 
					                    return (rounds, map);
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
                if matches!(map[p.0][p.1], Tile::Empty) {
 | 
					                if matches!(map[p.0][p.1], Tile::Empty) {
 | 
				
			||||||
                    check_next.insert(p);
 | 
					                    check_next.insert(p);
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user