Function aoc::y15d09::y15d09

source ·
pub fn y15d09(input: &str, part: u32) -> u32
Expand description

The solution for the day nine challenge.

We take the input as a string and the problem part as an integer as usual. We start by iterating over the lines and parsing the city names and the distances. We maintain a std::collections::HashMap of the city names with their values equal to another HashMap that contains the other cities and their distances. This way we can lookup the distance from one city to any other city. Then we get all of the permutations of the cities which gives us all of the possible trips that we could take and then look at them by two to give us each leg of the trip. Just get the distance for the trip and then add it to the std::collections::BinaryHeaps that we’ve been maintaining of the trip distances. If we’re on part one then get the value from the min-heap, otherwise the value from the max-heap.

§Example

// probably read this from the input file...
let input = "A to B = 10\nB to C = 15\nA to C = 25\n";
assert_eq!(y15d09(input, 1), 25);
assert_eq!(y15d09(input, 2), 40);