-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
91 lines (72 loc) · 2.21 KB
/
lib.rs
File metadata and controls
91 lines (72 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use common::Answer;
use itertools::Itertools;
use rustc_hash::FxHashMap;
type IntType = u16;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
struct Location<'a>(&'a str);
#[derive(Debug, Clone, Copy)]
struct Distance(IntType);
#[derive(Debug, Clone, Copy)]
struct Route<'a>(Location<'a>, Location<'a>, Distance);
fn parse(s: &str) -> impl Iterator<Item = Route<'_>> {
s.lines().map(|l| {
let mut parts = l.split_whitespace();
let src = Location(parts.next().unwrap());
let dst = Location(parts.nth(1).unwrap());
let dist = Distance(
parts
.nth(1)
.and_then(|s| s.parse::<IntType>().ok())
.unwrap(),
);
Route(src, dst, dist)
})
}
#[derive(Debug, Clone)]
struct Graph<'a>(FxHashMap<Location<'a>, FxHashMap<Location<'a>, Distance>>);
impl<'a> Graph<'a> {
fn new() -> Self {
Self(FxHashMap::default())
}
fn insert(&mut self, a: Location<'a>, b: Location<'a>, distance: Distance) {
self.0.entry(a).or_default().insert(b, distance);
self.0.entry(b).or_default().insert(a, distance);
}
fn locations(&self) -> impl Iterator<Item = Location<'a>> + '_ {
self.0.keys().copied()
}
fn get(&self, a: Location<'a>, b: Location<'a>) -> Option<Distance> {
self.0.get(&a).and_then(|h| h.get(&b)).copied()
}
}
fn distances(s: &str) -> Vec<IntType> {
let mut graph = Graph::new();
parse(s).for_each(|r| graph.insert(r.0, r.1, r.2));
let locations_count = graph.locations().count();
graph
.locations()
.permutations(locations_count)
.map(|locs| {
locs.windows(2)
.map(|w| graph.get(w[0], w[1]).unwrap().0)
.sum::<IntType>()
})
.collect()
}
pub fn step1(s: &str) -> Answer {
distances(s).into_iter().min().unwrap().into()
}
pub fn step2(s: &str) -> Answer {
distances(s).into_iter().max().unwrap().into()
}
#[cfg(test)]
mod test {
use super::*;
const INPUT: &str = r#"London to Dublin = 464
London to Belfast = 518
Dublin to Belfast = 141"#;
#[test]
fn step1_finds_correct_answer() {
assert_eq!(step1(INPUT), Answer::Unsigned(605));
}
}