summaryrefslogtreecommitdiff
path: root/src/bin
diff options
context:
space:
mode:
authorJustin Wernick <justin@worthe-it.co.za>2019-12-11 01:18:13 +0200
committerJustin Wernick <justin@worthe-it.co.za>2019-12-11 01:18:13 +0200
commit46eda1faf09a3b33d925ea1692d6b38029606c4c (patch)
tree794f78d8187b5b93a7e86f60e56d940fc27ebae5 /src/bin
parent9438066faf52464f8834035721733d82cdf4e322 (diff)
Input parsing for day 6
Diffstat (limited to 'src/bin')
-rw-r--r--src/bin/day_5.rs8
-rw-r--r--src/bin/day_6.rs77
2 files changed, 79 insertions, 6 deletions
diff --git a/src/bin/day_5.rs b/src/bin/day_5.rs
index e7539e1..a3cf869 100644
--- a/src/bin/day_5.rs
+++ b/src/bin/day_5.rs
@@ -11,17 +11,13 @@ use structopt::StructOpt;
type Intcode = i32;
#[derive(Debug, StructOpt)]
-#[structopt(name = "Day 2: 1202 Program Alarm")]
+#[structopt(name = "Day 5: Sunny with a Chance of Asteroids")]
/// Executes an Intcode program
///
/// The program is read from stdin as a series of comma-separated
/// values. Newlines are ignored.
///
-/// If an output is provided, all possible inputs are tried to find
-/// the input that results in the desired output. In this case, the
-/// inputs are returned in the format (noun, verb).
-///
-///See https://adventofcode.com/2019/day/5 for details.
+/// See https://adventofcode.com/2019/day/5 for details.
struct Opt {
#[structopt(short = "i", long = "input")]
input: Vec<Intcode>,
diff --git a/src/bin/day_6.rs b/src/bin/day_6.rs
new file mode 100644
index 0000000..a305a90
--- /dev/null
+++ b/src/bin/day_6.rs
@@ -0,0 +1,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
+}