2019-12-06 11:18:12 +01:00
|
|
|
use std::collections::HashMap;
|
2019-12-06 12:00:37 +01:00
|
|
|
use std::collections::HashSet;
|
2019-12-06 19:06:49 +01:00
|
|
|
use std::fmt::Write;
|
2019-12-06 11:45:15 +01:00
|
|
|
use std::iter;
|
2019-12-06 11:18:12 +01:00
|
|
|
|
2020-12-14 19:24:43 +01:00
|
|
|
use anyhow::{Context, Result};
|
2019-12-06 11:18:12 +01:00
|
|
|
|
|
|
|
const INPUT: &str = include_str!("../input/day06.txt");
|
|
|
|
|
2019-12-06 19:06:49 +01:00
|
|
|
pub fn run() -> Result<String> {
|
|
|
|
let mut res = String::with_capacity(128);
|
2019-12-06 11:18:12 +01:00
|
|
|
|
2019-12-06 19:06:49 +01:00
|
|
|
writeln!(res, "part 1: {}", part1(INPUT)?)?;
|
|
|
|
writeln!(res, "part 2: {}", part2(INPUT)?)?;
|
|
|
|
|
|
|
|
Ok(res)
|
2019-12-06 11:18:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn count_orbits(
|
|
|
|
key: &str,
|
|
|
|
orbits: &HashMap<String, String>,
|
|
|
|
cache: &mut HashMap<String, u64>,
|
|
|
|
) -> u64 {
|
|
|
|
match cache.get(key) {
|
|
|
|
Some(val) => *val,
|
|
|
|
None => {
|
|
|
|
let val = match orbits.get(key) {
|
|
|
|
Some(parent) => count_orbits(parent, orbits, cache) + 1,
|
|
|
|
None => 0,
|
|
|
|
};
|
|
|
|
cache.insert(key.to_string(), val);
|
|
|
|
val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn part1(input: &str) -> Result<u64> {
|
|
|
|
let orbits = input
|
|
|
|
.lines()
|
2020-12-17 01:23:10 +01:00
|
|
|
.map(str::trim_end)
|
2019-12-06 11:18:12 +01:00
|
|
|
.map(|line| {
|
|
|
|
let paren = line
|
|
|
|
.find(')')
|
2020-12-14 19:24:43 +01:00
|
|
|
.with_context(|| format!("couldn't find `)` in line: {}", line))?;
|
2019-12-06 11:18:12 +01:00
|
|
|
Ok((line[paren + 1..].to_string(), line[..paren].to_string()))
|
|
|
|
})
|
|
|
|
.collect::<Result<HashMap<String, String>>>()?;
|
|
|
|
|
|
|
|
let mut cache = HashMap::new();
|
|
|
|
Ok(orbits
|
|
|
|
.keys()
|
2021-11-25 16:23:22 +01:00
|
|
|
.map(|k| count_orbits(k, &orbits, &mut cache))
|
2019-12-06 11:18:12 +01:00
|
|
|
.sum())
|
|
|
|
}
|
|
|
|
|
2019-12-06 11:45:15 +01:00
|
|
|
fn part2(input: &str) -> Result<usize> {
|
|
|
|
let orbits = input
|
|
|
|
.lines()
|
2020-12-17 01:23:10 +01:00
|
|
|
.map(str::trim_end)
|
2019-12-06 11:45:15 +01:00
|
|
|
.map(|line| {
|
|
|
|
let paren = line
|
|
|
|
.find(')')
|
2020-12-14 19:24:43 +01:00
|
|
|
.with_context(|| format!("couldn't find `)` in line: {}", line))?;
|
2019-12-06 11:45:15 +01:00
|
|
|
Ok((line[paren + 1..].to_string(), line[..paren].to_string()))
|
|
|
|
})
|
|
|
|
.collect::<Result<HashMap<String, String>>>()?;
|
|
|
|
|
2019-12-07 00:31:10 +01:00
|
|
|
let succ = |key: &String| orbits.get(key).cloned();
|
2019-12-06 11:45:15 +01:00
|
|
|
|
2019-12-06 12:00:37 +01:00
|
|
|
let you_path = iter::successors(Some("YOU".to_string()), succ).collect::<HashSet<_>>();
|
|
|
|
let santa_path = iter::successors(Some("SAN".to_string()), succ).collect::<HashSet<_>>();
|
2019-12-06 11:45:15 +01:00
|
|
|
|
2019-12-06 12:00:37 +01:00
|
|
|
Ok(you_path.symmetric_difference(&santa_path).count() - 2)
|
2019-12-06 11:45:15 +01:00
|
|
|
}
|
|
|
|
|
2019-12-06 11:18:12 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
const PROVIDED1: &str = "COM)B
|
|
|
|
B)C
|
|
|
|
C)D
|
|
|
|
D)E
|
|
|
|
E)F
|
|
|
|
B)G
|
|
|
|
G)H
|
|
|
|
D)I
|
|
|
|
E)J
|
|
|
|
J)K
|
|
|
|
K)L
|
2019-12-06 11:45:15 +01:00
|
|
|
";
|
|
|
|
|
|
|
|
const PROVIDED2: &str = "COM)B
|
|
|
|
B)C
|
|
|
|
C)D
|
|
|
|
D)E
|
|
|
|
E)F
|
|
|
|
B)G
|
|
|
|
G)H
|
|
|
|
D)I
|
|
|
|
E)J
|
|
|
|
J)K
|
|
|
|
K)L
|
|
|
|
K)YOU
|
|
|
|
I)SAN
|
2019-12-06 11:18:12 +01:00
|
|
|
";
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn part1_provided() {
|
|
|
|
assert_eq!(part1(PROVIDED1).unwrap(), 42);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn part1_real() {
|
|
|
|
assert_eq!(part1(INPUT).unwrap(), 140608);
|
|
|
|
}
|
2019-12-06 11:45:15 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn part2_provided() {
|
|
|
|
assert_eq!(part2(PROVIDED2).unwrap(), 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn part2_real() {
|
|
|
|
assert_eq!(part2(INPUT).unwrap(), 337);
|
|
|
|
}
|
2019-12-06 11:18:12 +01:00
|
|
|
}
|