Skip to content

Commit

Permalink
feat: aoc2022-12 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
msyfls123 committed Dec 26, 2023
1 parent 027d1a6 commit 54164ff
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions advent_of_code/src/aoc2022/12.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,29 @@ fn find_start(map: &HeightMap) -> Position {
}).unwrap().to_owned()
}

fn shortest_path(map: &HeightMap, size: (usize, usize)) -> Option<usize> {
fn shortest_path(
map: &HeightMap,
size: (usize, usize),
start_points: Vec<Position>
) -> Option<usize> {
let mut dist: DistanceMap = HashMap::new();
for (&pos, _) in map {
dist.insert(pos, usize::MAX);
}

let mut heap = BinaryHeap::new();
let start = find_start(map);
let start_point = find_start(map);

for start in start_points {

let start_entry = dist.get_mut(&start).unwrap();
*start_entry = 0;
heap.push(Point{ steps: vec!{}, position: start });
}

let start_entry = dist.get_mut(&start).unwrap();
*start_entry = 0;
heap.push(Point{ steps: vec!{}, position: start });

while let Some(Point { steps, position }) = heap.pop() {
let val = if position == start {
let val = if position == start_point {
'a'
} else {
*map.get(&position).unwrap()
Expand Down Expand Up @@ -116,6 +124,18 @@ fn main() {
let data = get_str_array_from_file(&vec!{"aoc2022", "data", "12.txt"});
let map = parse_height_map(&data);
let size = (data[0].len(), data.len());
let steps = shortest_path(&map, size);
let start = find_start(&map);
let steps = shortest_path(&map, size, vec!{start});
println!("Part 1: {:?}", steps);

let all_a_squares = map.iter().filter_map(|(pos, val)| {
if val == &'a' {
Some(*pos)
} else {
None
}
}).collect();
let steps = shortest_path(&map, size, all_a_squares);
println!("Part 2: {:?}", steps);

}

0 comments on commit 54164ff

Please sign in to comment.