aoc/year2015/
day09.rs

1//! # All in a Single Night
2//!
3//! This is a variant of the classic NP-hard
4//! [Travelling Salesman Problem](https://en.wikipedia.org/wiki/Travelling_salesman_problem).
5//!
6//! There are 8 locations, so naively it would require checking 8! = 40,320 permutations. We can
7//! reduce this to 7! = 5,040 permutations by arbitrarily choosing one of the locations as the
8//! start.
9//!
10//! We then compute the distance to complete the trip and return to the original location.
11//! Since the problem does not ask us to end up in the same location we then "break" the cycle.
12//! To compute the shortest journey we remove the longest single journey and to compute the
13//! longest journey we remove the shortest single journey.
14//!
15//! For speed we first convert each location into an index, then store the distances between
16//! every pair of locations in an array for fast lookup. Our utility [`permutations`] method uses
17//! [Heap's algorithm](https://en.wikipedia.org/wiki/Heap%27s_algorithm) for efficiency,
18//! modifying the slice in place.
19//!
20//! [`permutations`]: crate::util::slice
21use crate::util::hash::*;
22use crate::util::iter::*;
23use crate::util::parse::*;
24use crate::util::slice::*;
25
26type Result = (u32, u32);
27
28pub fn parse(input: &str) -> Result {
29    let tokens: Vec<_> = input.split_ascii_whitespace().chunk::<5>().collect();
30    let mut indices = FastMap::new();
31
32    for [start, _, end, ..] in &tokens {
33        let size = indices.len();
34        indices.entry(start).or_insert(size);
35
36        let size = indices.len();
37        indices.entry(end).or_insert(size);
38    }
39
40    let stride = indices.len();
41    let mut distances = vec![0; stride * stride];
42
43    for [start, _, end, _, distance] in &tokens {
44        let start = indices[start];
45        let end = indices[end];
46        let distance = distance.unsigned();
47
48        distances[stride * start + end] = distance;
49        distances[stride * end + start] = distance;
50    }
51
52    let mut global_min = u32::MAX;
53    let mut global_max = u32::MIN;
54    let mut indices: Vec<_> = (1..stride).collect();
55
56    indices.permutations(|slice| {
57        let mut sum = 0;
58        let mut local_min = u32::MAX;
59        let mut local_max = u32::MIN;
60
61        let mut trip = |from, to| {
62            let distance = distances[stride * from + to];
63            sum += distance;
64            local_min = local_min.min(distance);
65            local_max = local_max.max(distance);
66        };
67
68        trip(0, slice[0]);
69        trip(0, slice[slice.len() - 1]);
70
71        for i in 1..slice.len() {
72            trip(slice[i], slice[i - 1]);
73        }
74
75        global_min = global_min.min(sum - local_max);
76        global_max = global_max.max(sum - local_min);
77    });
78
79    (global_min, global_max)
80}
81
82pub fn part1(input: &Result) -> u32 {
83    input.0
84}
85
86pub fn part2(input: &Result) -> u32 {
87    input.1
88}