2019: day06: part 1
This commit is contained in:
parent
127cf3f7c4
commit
1f364cd20f
4 changed files with 982 additions and 1 deletions
77
aoc2019/src/day06.rs
Normal file
77
aoc2019/src/day06.rs
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
use aoc::err;
|
||||
use aoc::Result;
|
||||
|
||||
const INPUT: &str = include_str!("../input/day06.txt");
|
||||
|
||||
pub fn run() -> Result<()> {
|
||||
println!("part 1: {}", part1(INPUT)?);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
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()
|
||||
.map(|line| line.trim_end())
|
||||
.map(|line| {
|
||||
let paren = line
|
||||
.find(')')
|
||||
.ok_or_else(|| err!("couldn't find `)` in line: {}", line))?;
|
||||
Ok((line[paren + 1..].to_string(), line[..paren].to_string()))
|
||||
})
|
||||
.collect::<Result<HashMap<String, String>>>()?;
|
||||
|
||||
let mut cache = HashMap::new();
|
||||
Ok(orbits
|
||||
.keys()
|
||||
.map(|k| count_orbits(&k, &orbits, &mut cache))
|
||||
.sum())
|
||||
}
|
||||
|
||||
#[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
|
||||
";
|
||||
|
||||
#[test]
|
||||
fn part1_provided() {
|
||||
assert_eq!(part1(PROVIDED1).unwrap(), 42);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part1_real() {
|
||||
assert_eq!(part1(INPUT).unwrap(), 140608);
|
||||
}
|
||||
}
|
||||
|
|
@ -3,3 +3,4 @@ pub mod day02;
|
|||
pub mod day03;
|
||||
pub mod day04;
|
||||
pub mod day05;
|
||||
pub mod day06;
|
||||
|
|
|
|||
|
|
@ -5,9 +5,17 @@ use aoc2019::day02;
|
|||
use aoc2019::day03;
|
||||
use aoc2019::day04;
|
||||
use aoc2019::day05;
|
||||
use aoc2019::day06;
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let days: &[fn() -> Result<()>] = &[day01::run, day02::run, day03::run, day04::run, day05::run];
|
||||
let days: &[fn() -> Result<()>] = &[
|
||||
day01::run,
|
||||
day02::run,
|
||||
day03::run,
|
||||
day04::run,
|
||||
day05::run,
|
||||
day06::run,
|
||||
];
|
||||
|
||||
aoc::run(days)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue