done with part2

This commit is contained in:
Joe Ardent 2022-12-13 13:32:17 -08:00
parent 2cd0dce596
commit a62ed3ec13

View file

@ -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);
}
}