Skip to content

Commit eaa80da

Browse files
committed
feat(09/2015): solve second part
1 parent 4b1258f commit eaa80da

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
| [Day 6: Probably a Fire Hazard](src/solutions/year2015/day06.rs) | ⭐⭐ | 871.538 | 817.533 |
9696
| [Day 7: Some Assembly Required](src/solutions/year2015/day07.rs) | ⭐⭐ | 0.308 | 0.293 |
9797
| [Day 8: Matchsticks](src/solutions/year2015/day08.rs) | ⭐⭐ | 0.052 | 0.129 |
98-
| [Day 9: All in a Single Night](src/solutions/year2015/day09.rs) | | 34.320 | - |
98+
| [Day 9: All in a Single Night](src/solutions/year2015/day09.rs) | | 34.320 | 34.85 |
9999

100100
# TODO
101101

src/solutions/year2015/day09.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@ impl Solution for Day09 {
1212
.to_string()
1313
}
1414

15-
fn part_two(&self, _input: &str) -> String {
16-
String::from("0")
15+
fn part_two(&self, input: &str) -> String {
16+
self.parse(input)
17+
.find_longest_path_cost()
18+
.unwrap()
19+
.to_string()
1720
}
1821
}
1922

@@ -47,4 +50,9 @@ Dublin to Belfast = 141"#;
4750
fn part_one_example_test() {
4851
assert_eq!("605", Day09.part_one(EXAMPLE));
4952
}
53+
54+
#[test]
55+
fn part_two_example_test() {
56+
assert_eq!("982", Day09.part_two(EXAMPLE));
57+
}
5058
}

src/utils/graphs/graph.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl<T> Graph<T> {
6666
fn edges(&self) -> &HashSet<(T, T)> {
6767
&self.edges
6868
}
69-
69+
7070
pub fn nodes(&self) -> &HashSet<T> {
7171
&self.nodes
7272
}

src/utils/graphs/travelling_salesman.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,32 @@ impl<T: PartialEq + Eq + Hash> TravellingSalesman<T> {
1919
}
2020

2121
pub fn find_shortest_path_cost(&self) -> Option<usize>
22+
where
23+
T: PartialEq + Clone,
24+
{
25+
self.all_paths().iter().map(|path| path.cost).min()
26+
}
27+
28+
pub fn find_longest_path_cost(&self) -> Option<usize>
29+
where
30+
T: PartialEq + Clone,
31+
{
32+
self.all_paths().iter().map(|path| path.cost).max()
33+
}
34+
35+
fn all_paths(&self) -> Vec<Path<T>>
2236
where
2337
T: PartialEq + Clone,
2438
{
2539
self.graph
2640
.nodes()
2741
.iter()
2842
.tuple_combinations()
29-
.map(|(from, to)| self.shortest_path(from, to))
30-
.min()
43+
.flat_map(|(from, to)| self.all_paths_between(from, to))
44+
.collect()
3145
}
3246

33-
fn shortest_path(&self, from: &T, to: &T) -> usize
47+
fn all_paths_between(&self, from: &T, to: &T) -> Vec<Path<T>>
3448
where
3549
T: PartialEq + Clone,
3650
{
@@ -64,7 +78,7 @@ impl<T: PartialEq + Eq + Hash> TravellingSalesman<T> {
6478
}
6579
}
6680

67-
finished_paths.iter().map(|path| path.cost).min().unwrap()
81+
finished_paths
6882
}
6983
}
7084

0 commit comments

Comments
 (0)