done with part2
This commit is contained in:
parent
2cd0dce596
commit
a62ed3ec13
1 changed files with 18 additions and 4 deletions
|
@ -12,7 +12,7 @@ fn parse_input(input: &str) -> (NodeIndex, NodeIndex, GRAPH) {
|
||||||
let mut s = NodeIndex::default();
|
let mut s = NodeIndex::default();
|
||||||
let mut e = 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());
|
let row = Vec::from_iter(line.chars());
|
||||||
g.push(row);
|
g.push(row);
|
||||||
}
|
}
|
||||||
|
@ -99,8 +99,22 @@ fn part1((start, end, graph): &(NodeIndex, NodeIndex, GRAPH)) -> i32 {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[aoc_run(day12, part2)]
|
#[aoc_run(day12, part2)]
|
||||||
fn part2(input: &(NodeIndex, NodeIndex, GRAPH)) -> u32 {
|
fn part2((start, end, graph): &(NodeIndex, NodeIndex, GRAPH)) -> i32 {
|
||||||
0
|
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)]
|
#[cfg(test)]
|
||||||
|
@ -122,6 +136,6 @@ abdefghi";
|
||||||
#[test]
|
#[test]
|
||||||
fn part2_test() {
|
fn part2_test() {
|
||||||
let v = parse_input(INPUT);
|
let v = parse_input(INPUT);
|
||||||
assert_eq!(part2(&v), 1);
|
assert_eq!(part2(&v), 29);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue