day8: part2 solution

This commit is contained in:
2025-12-08 01:04:14 -08:00
parent 41f9e971ee
commit c97f31e0fd

View File

@@ -126,7 +126,23 @@ fn part1(input: &Circuits) -> u64 {
#[aoc(day8, part2)]
fn part2(input: &Circuits) -> u64 {
0
let mut circuits = input.clone();
circuits.circuits = Vec::from_iter((0..circuits.junctions.len()).map(|i| vec![i]));
for (a, b, _d) in circuits
.junctions
.iter()
.enumerate()
.combinations(2)
.map(|p| (p[0].0, p[1].0, p[0].1.distance(p[1].1)))
.sorted_by(|a, b| a.2.partial_cmp(&b.2).unwrap())
{
circuits.connect(a, b);
if circuits.circuits.len() == 1 {
return (circuits.junctions[a].pos.0 * circuits.junctions[b].pos.0) as u64;
}
}
panic!()
}
#[cfg(test)]
@@ -161,6 +177,6 @@ mod tests {
#[test]
fn part2_example() {
assert_eq!(part2(&parse(EXAMPLE)), 0);
assert_eq!(part2(&parse(EXAMPLE)), 25272);
}
}