summaryrefslogtreecommitdiff
path: root/src/bin/day_6.rs
blob: a305a9085410265e1434d24c0dc274a49a1eac6b (plain)
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
use std::fmt;
use std::io;
use std::io::prelude::*;
use std::process;
use std::str::FromStr;
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
#[structopt(name = "Day 6: Universal Orbit Map")]
/// Counts the total number of direct and indirect orbits between planets.
///
/// Input is read from stdin, one direct orbit per line, in the format
/// `A)B` (B is orbiting A).
///
/// See https://adventofcode.com/2019/day/6 for details.
struct Opt {}

fn main() {
    let stdin = io::stdin();
    let opt = Opt::from_args();

    let orbits = stdin
        .lock()
        .lines()
        .map(|x| exit_on_failed_assertion(x, "Error reading input"))
        .map(|x| exit_on_failed_assertion(x.parse::<Orbit>(), "Input was not a valid orbit"));

    println!("{}", count_orbits(orbits));
}

fn exit_on_failed_assertion<A, E: std::error::Error>(data: Result<A, E>, message: &str) -> A {
    match data {
        Ok(data) => data,
        Err(e) => {
            eprintln!("{}: {}", message, e);
            process::exit(1);
        }
    }
}

#[derive(Debug)]
struct StrError {
    str: String,
}

impl fmt::Display for StrError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.str)
    }
}
impl std::error::Error for StrError {}

struct Orbit {
    a: String,
    b: String,
}

impl FromStr for Orbit {
    type Err = StrError;

    fn from_str(s: &str) -> Result<Self, StrError> {
        match s.split(')').collect::<Vec<_>>()[..] {
            [a, b] => Ok(Orbit {
                a: a.to_string(),
                b: b.to_string(),
            }),
            _ => Err(StrError {
                str: format!("{} is not a valid orbit description", s),
            }),
        }
    }
}

fn count_orbits(it: impl Iterator<Item = Orbit>) -> usize {
    // TODO
    0
}