summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJustin Wernick <justin@worthe-it.co.za>2019-12-31 20:45:13 +0200
committerJustin Wernick <justin@worthe-it.co.za>2019-12-31 20:45:13 +0200
commit832190583eacad6d0166cbacb71f79fa93aef1d6 (patch)
tree5adc66aa63e04fc1af4936d6a63fffbb9adf2c17 /src
parentfc4aef0eb26fcd64a91f95e5aa8e9cbc800b3ba8 (diff)
Day 21 hello world, springbot bouncing into a hole
Diffstat (limited to 'src')
-rw-r--r--src/bin/day_17.rs7
-rw-r--r--src/bin/day_21.rs62
2 files changed, 62 insertions, 7 deletions
diff --git a/src/bin/day_17.rs b/src/bin/day_17.rs
index 9f5d3b8..e85373c 100644
--- a/src/bin/day_17.rs
+++ b/src/bin/day_17.rs
@@ -1,13 +1,6 @@
use aoc2019::*;
-use rpds::list;
-use rpds::list::List;
-use rpds::rbt_set;
-use rpds::set::red_black_tree_set::RedBlackTreeSet;
-use rpds::vector;
-use rpds::vector::Vector;
use std::io;
use std::io::prelude::*;
-use std::iter;
use std::process;
use structopt::StructOpt;
diff --git a/src/bin/day_21.rs b/src/bin/day_21.rs
new file mode 100644
index 0000000..106ce82
--- /dev/null
+++ b/src/bin/day_21.rs
@@ -0,0 +1,62 @@
+use aoc2019::*;
+use std::io;
+use std::io::prelude::*;
+use std::process;
+use structopt::StructOpt;
+
+#[derive(Debug, StructOpt)]
+#[structopt(name = "Day 21: Springdroid Adventure")]
+/// Pilots a springdroid around!
+///
+/// See https://adventofcode.com/2019/day/21 for details.
+struct Opt {}
+
+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::<Intcode>(), "Invalid number"))
+ .collect::<IntcodeProgram>();
+
+ let result = exit_on_failed_assertion(
+ {
+ let input = vec!["NOT A J\n", "WALK\n"];
+ program
+ .with_input(
+ input
+ .iter()
+ .flat_map(|line| line.chars().map(|c| Intcode::from(c as u8)))
+ .collect(),
+ )
+ .execute()
+ },
+ "Program failed",
+ );
+
+ println!(
+ "{}",
+ result
+ .drop_last()
+ .unwrap()
+ .iter()
+ .flat_map(|c| c.to_signed_bytes_be())
+ .map(|c| c as char)
+ .collect::<String>()
+ );
+ println!("{}", result.last().unwrap());
+}
+
+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);
+ }
+ }
+}