From a62ed3ec135437bbf313bd38a4aa9dbaf11f2461 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Tue, 13 Dec 2022 13:32:17 -0800 Subject: [PATCH] done with part2 --- 2022-aoc/src/d12.rs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/2022-aoc/src/d12.rs b/2022-aoc/src/d12.rs index 88f80f0..39e1a87 100644 --- a/2022-aoc/src/d12.rs +++ b/2022-aoc/src/d12.rs @@ -12,7 +12,7 @@ fn parse_input(input: &str) -> (NodeIndex, NodeIndex, GRAPH) { let mut s = NodeIndex::default(); let mut e = NodeIndex::default(); - for (ridx, line) in input.lines().enumerate() { + for line in input.lines() { let row = Vec::from_iter(line.chars()); g.push(row); } @@ -99,8 +99,22 @@ fn part1((start, end, graph): &(NodeIndex, NodeIndex, GRAPH)) -> i32 { } #[aoc_run(day12, part2)] -fn part2(input: &(NodeIndex, NodeIndex, GRAPH)) -> u32 { - 0 +fn part2((start, end, graph): &(NodeIndex, NodeIndex, GRAPH)) -> i32 { + let mut roots = Vec::new(); + roots.push(*start); + for (i, w) in graph.node_weights().enumerate() { + if w == &'a' { + roots.push(NodeIndex::new(i)); + } + } + let mut res = Vec::with_capacity(roots.len()); + for root in roots { + let p = dijkstra(graph, root, Some(*end), |_| 1); + if let Some(v) = p.get(end) { + res.push(*v) + } + } + *res.iter().min().unwrap() } #[cfg(test)] @@ -122,6 +136,6 @@ abdefghi"; #[test] fn part2_test() { let v = parse_input(INPUT); - assert_eq!(part2(&v), 1); + assert_eq!(part2(&v), 29); } }