Skip to content

Commit 67761cf

Browse files
committed
feat(09/2015): implement parse
1 parent 3fc75d8 commit 67761cf

File tree

1 file changed

+51
-3
lines changed

1 file changed

+51
-3
lines changed

src/solutions/year2015/day09.rs

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,71 @@
11
use crate::solutions::Solution;
2+
use itertools::Itertools;
3+
use std::collections::HashMap;
4+
5+
type Distances = HashMap<(String, String), u64>;
26

37
pub struct Day09;
48

59
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()
824
}
925

1026
fn part_two(&self, _input: &str) -> String {
1127
String::from("0")
1228
}
1329
}
1430

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+
1559
#[cfg(test)]
1660
mod tests {
1761
use super::*;
1862

63+
const EXAMPLE: &str = r#"London to Dublin = 464
64+
London to Belfast = 518
65+
Dublin to Belfast = 141"#;
66+
1967
#[test]
2068
fn part_one_example_test() {
21-
assert_eq!("0", Day09.part_one(""));
69+
assert_eq!("605", Day09.part_one(EXAMPLE));
2270
}
2371
}

0 commit comments

Comments
 (0)