From 9892e3ebde304726903a1e5c358d05c2e343ea5e Mon Sep 17 00:00:00 2001 From: Justin Wernick Date: Tue, 19 Apr 2022 20:26:36 +0200 Subject: Refile for merging repos --- 2019/src/bin/day_5.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 2019/src/bin/day_5.rs (limited to '2019/src/bin/day_5.rs') diff --git a/2019/src/bin/day_5.rs b/2019/src/bin/day_5.rs new file mode 100644 index 0000000..07f7af8 --- /dev/null +++ b/2019/src/bin/day_5.rs @@ -0,0 +1,45 @@ +use aoc2019::*; +use std::io; +use std::io::prelude::*; +use std::process; +use structopt::StructOpt; + +#[derive(Debug, StructOpt)] +#[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. +/// +/// See https://adventofcode.com/2019/day/5 for details. +struct Opt { + #[structopt(short = "i", long = "input")] + input: Vec, +} + +fn main() { + let stdin = io::stdin(); + let opt = Opt::from_args(); + + let program: IntcodeProgram = stdin + .lock() + .split(b',') + .map(|x| exit_on_failed_assertion(x, "Error reading input")) + .map(|x| exit_on_failed_assertion(String::from_utf8(x), "Input was not valid UTF-8")) + .map(|x| exit_on_failed_assertion(x.trim().parse::(), "Invalid number")) + .collect::() + .with_input(opt.input.into_iter().collect()); + + let result = exit_on_failed_assertion(program.execute(), "Program errored"); + println!("{}", result); +} + +fn exit_on_failed_assertion(data: Result, message: &str) -> A { + match data { + Ok(data) => data, + Err(e) => { + eprintln!("{}: {}", message, e); + process::exit(1); + } + } +} -- cgit v1.2.3