Solutions for 2022, as well as 2015-2018 and 2019 up to day 11
This commit is contained in:
commit
1895197c49
722 changed files with 375457 additions and 0 deletions
8
2015/day09-all_in_a_single_night/Cargo.toml
Normal file
8
2015/day09-all_in_a_single_night/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "day09-all_in_a_single_night"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
52
2015/day09-all_in_a_single_night/challenge.txt
Normal file
52
2015/day09-all_in_a_single_night/challenge.txt
Normal file
|
@ -0,0 +1,52 @@
|
|||
\--- Day 9: All in a Single Night ---
|
||||
----------
|
||||
|
||||
Every year, Santa manages to deliver all of his presents in a single night.
|
||||
|
||||
This year, however, he has some new locations to visit; his elves have provided him the distances between every pair of locations. He can start and end at any two (different) locations he wants, but he must visit each location exactly once. What is the *shortest distance* he can travel to achieve this?
|
||||
|
||||
For example, given the following distances:
|
||||
|
||||
```
|
||||
London to Dublin = 464
|
||||
London to Belfast = 518
|
||||
Dublin to Belfast = 141
|
||||
|
||||
```
|
||||
|
||||
The possible routes are therefore:
|
||||
|
||||
```
|
||||
Dublin -> London -> Belfast = 982
|
||||
London -> Dublin -> Belfast = 605
|
||||
London -> Belfast -> Dublin = 659
|
||||
Dublin -> Belfast -> London = 659
|
||||
Belfast -> Dublin -> London = 605
|
||||
Belfast -> London -> Dublin = 982
|
||||
|
||||
```
|
||||
|
||||
The shortest of these is `London -> Dublin -> Belfast = 605`, and so the answer is `605` in this example.
|
||||
|
||||
What is the distance of the shortest route?
|
||||
|
||||
Your puzzle answer was `117`.
|
||||
|
||||
\--- Part Two ---
|
||||
----------
|
||||
|
||||
The next year, just to show off, Santa decides to take the route with the *longest distance* instead.
|
||||
|
||||
He can still start and end at any two (different) locations he wants, and he still must visit each location exactly once.
|
||||
|
||||
For example, given the distances above, the longest route would be `982` via (for example) `Dublin -> London -> Belfast`.
|
||||
|
||||
What is the distance of the longest route?
|
||||
|
||||
Your puzzle answer was `909`.
|
||||
|
||||
Both parts of this puzzle are complete! They provide two gold stars: \*\*
|
||||
|
||||
At this point, all that is left is for you to [admire your Advent calendar](/2015).
|
||||
|
||||
If you still want to see it, you can [get your puzzle input](9/input).
|
72
2015/day09-all_in_a_single_night/src/lib.rs
Normal file
72
2015/day09-all_in_a_single_night/src/lib.rs
Normal file
|
@ -0,0 +1,72 @@
|
|||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
pub fn run(input: &str) -> (usize, usize) {
|
||||
let distances = get_distances(input);
|
||||
let first = try_all(&distances, &{|a, b| b.cmp(a)});
|
||||
let second = try_all(&distances, &{|a, b| a.cmp(b)});
|
||||
(first, second)
|
||||
}
|
||||
|
||||
fn get_distances(input: &str) -> HashMap<(u16, u16), usize> {
|
||||
let mut cities = HashMap::new();
|
||||
let mut map = HashMap::new();
|
||||
|
||||
input.lines().for_each(|line| {
|
||||
let components: Vec<&str> = line.split(' ').collect();
|
||||
assert_eq!(components.len(), 5);
|
||||
|
||||
let next = 2_u16.pow(cities.len() as u32);
|
||||
let from = *cities.entry(components[0]).or_insert(next);
|
||||
let next = 2_u16.pow(cities.len() as u32);
|
||||
let to = *cities.entry(components[2]).or_insert(next);
|
||||
let distance = components[4].parse().unwrap();
|
||||
|
||||
map.insert((from, to), distance);
|
||||
map.insert((to, from), distance);
|
||||
});
|
||||
|
||||
map
|
||||
}
|
||||
|
||||
fn try_all<F>(distances: &HashMap<(u16, u16), usize>, comparison: &F) -> usize
|
||||
where F: Fn(&usize, &usize) -> std::cmp::Ordering
|
||||
{
|
||||
let starting_points: HashSet<u16> = distances.keys().map(|(from, _)| *from).collect();
|
||||
|
||||
starting_points.iter()
|
||||
.map(|&from| try_all_from(from, distances, from, comparison))
|
||||
.max_by(comparison)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn try_all_from<F>(current: u16, distances: &HashMap<(u16, u16), usize>, visited: u16, comparison: &F) -> usize
|
||||
where F: Fn(&usize, &usize) -> std::cmp::Ordering
|
||||
{
|
||||
distances.keys()
|
||||
.filter(|(from, to)| *from == current && *to & visited == 0)
|
||||
.map(|(_, to)| distances.get(&(current, *to)).unwrap() + try_all_from(*to, distances, visited | to, comparison))
|
||||
.max_by(comparison)
|
||||
.unwrap_or(0)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::fs::read_to_string;
|
||||
|
||||
fn read_file(name: &str) -> String {
|
||||
read_to_string(name).expect(&format!("Unable to read file: {}", name)[..])
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sample() {
|
||||
let sample_input = read_file("tests/sample_input");
|
||||
assert_eq!(run(&sample_input), (605, 982));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_challenge() {
|
||||
let challenge_input = read_file("tests/challenge_input");
|
||||
assert_eq!(run(&challenge_input), (117, 909));
|
||||
}
|
||||
}
|
28
2015/day09-all_in_a_single_night/tests/challenge_input
Normal file
28
2015/day09-all_in_a_single_night/tests/challenge_input
Normal file
|
@ -0,0 +1,28 @@
|
|||
Faerun to Tristram = 65
|
||||
Faerun to Tambi = 129
|
||||
Faerun to Norrath = 144
|
||||
Faerun to Snowdin = 71
|
||||
Faerun to Straylight = 137
|
||||
Faerun to AlphaCentauri = 3
|
||||
Faerun to Arbre = 149
|
||||
Tristram to Tambi = 63
|
||||
Tristram to Norrath = 4
|
||||
Tristram to Snowdin = 105
|
||||
Tristram to Straylight = 125
|
||||
Tristram to AlphaCentauri = 55
|
||||
Tristram to Arbre = 14
|
||||
Tambi to Norrath = 68
|
||||
Tambi to Snowdin = 52
|
||||
Tambi to Straylight = 65
|
||||
Tambi to AlphaCentauri = 22
|
||||
Tambi to Arbre = 143
|
||||
Norrath to Snowdin = 8
|
||||
Norrath to Straylight = 23
|
||||
Norrath to AlphaCentauri = 136
|
||||
Norrath to Arbre = 115
|
||||
Snowdin to Straylight = 101
|
||||
Snowdin to AlphaCentauri = 84
|
||||
Snowdin to Arbre = 96
|
||||
Straylight to AlphaCentauri = 107
|
||||
Straylight to Arbre = 14
|
||||
AlphaCentauri to Arbre = 46
|
3
2015/day09-all_in_a_single_night/tests/sample_input
Normal file
3
2015/day09-all_in_a_single_night/tests/sample_input
Normal file
|
@ -0,0 +1,3 @@
|
|||
London to Dublin = 464
|
||||
London to Belfast = 518
|
||||
Dublin to Belfast = 141
|
Loading…
Add table
Add a link
Reference in a new issue