|
1 | 1 | use crate::solutions::Solution; |
| 2 | +use itertools::Itertools; |
| 3 | +use std::collections::HashMap; |
| 4 | + |
| 5 | +type Distances = HashMap<(String, String), u64>; |
2 | 6 |
|
3 | 7 | pub struct Day09; |
4 | 8 |
|
5 | 9 | impl Solution for Day09 { |
6 | | - fn part_one(&self, _input: &str) -> String { |
7 | | - String::from("0") |
| 10 | + fn part_one(&self, input: &str) -> String { |
| 11 | + let distances = self.parse(input); |
| 12 | + let cities = self.cities(&distances); |
| 13 | + |
| 14 | + println!("cities: {:?}", cities); |
| 15 | + println!("distances: {:?}", distances); |
| 16 | + |
| 17 | + cities |
| 18 | + .iter() |
| 19 | + .tuple_combinations() |
| 20 | + .map(|(_, _)| 0) |
| 21 | + .min() |
| 22 | + .unwrap() |
| 23 | + .to_string() |
8 | 24 | } |
9 | 25 |
|
10 | 26 | fn part_two(&self, _input: &str) -> String { |
11 | 27 | String::from("0") |
12 | 28 | } |
13 | 29 | } |
14 | 30 |
|
| 31 | +impl Day09 { |
| 32 | + fn parse(&self, input: &str) -> Distances { |
| 33 | + input |
| 34 | + .lines() |
| 35 | + .flat_map(|line| { |
| 36 | + let parts = line.split_whitespace().collect_vec(); |
| 37 | + |
| 38 | + let from = parts[0]; |
| 39 | + let to = parts[2]; |
| 40 | + let distance = parts[4].parse::<u64>().unwrap(); |
| 41 | + |
| 42 | + [ |
| 43 | + ((from.to_string(), to.to_string()), distance), |
| 44 | + ((to.to_string(), from.to_string()), distance), |
| 45 | + ] |
| 46 | + }) |
| 47 | + .collect() |
| 48 | + } |
| 49 | + |
| 50 | + fn cities(&self, distances: &Distances) -> Vec<String> { |
| 51 | + distances |
| 52 | + .keys() |
| 53 | + .flat_map(|(from, to)| [from.to_string(), to.to_string()]) |
| 54 | + .unique() |
| 55 | + .collect() |
| 56 | + } |
| 57 | +} |
| 58 | + |
15 | 59 | #[cfg(test)] |
16 | 60 | mod tests { |
17 | 61 | use super::*; |
18 | 62 |
|
| 63 | + const EXAMPLE: &str = r#"London to Dublin = 464 |
| 64 | +London to Belfast = 518 |
| 65 | +Dublin to Belfast = 141"#; |
| 66 | + |
19 | 67 | #[test] |
20 | 68 | fn part_one_example_test() { |
21 | | - assert_eq!("0", Day09.part_one("")); |
| 69 | + assert_eq!("605", Day09.part_one(EXAMPLE)); |
22 | 70 | } |
23 | 71 | } |
0 commit comments