diff options
author | Justin Wernick <justin@worthe-it.co.za> | 2019-12-24 23:54:53 +0200 |
---|---|---|
committer | Justin Wernick <justin@worthe-it.co.za> | 2019-12-24 23:54:53 +0200 |
commit | 65d0a677dfed91d8d08bf6961e8c418a1468566d (patch) | |
tree | dc7ad15e0eabc374842c388855397678249727e6 /src | |
parent | 8039de357a6e8e0e5d4c303ba56b18c57521e60e (diff) |
Day 17: The vacuum robot
Diffstat (limited to 'src')
-rw-r--r-- | src/bin/day_17.rs | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/src/bin/day_17.rs b/src/bin/day_17.rs new file mode 100644 index 0000000..9f5d3b8 --- /dev/null +++ b/src/bin/day_17.rs @@ -0,0 +1,85 @@ +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; + +#[derive(Debug, StructOpt)] +#[structopt(name = "Day 17: Set and Forget")] +/// Pilots a vacuum robot around on a scaffold. What could go wrong? +/// +/// See https://adventofcode.com/2019/day/17 for details. +struct Opt { + /// Draw the map and exit + #[structopt(short = "d")] + draw_map: bool, +} + +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( + if opt.draw_map { + program.execute() + } else { + // L,12,L,8,R,10,R,10,L,6,L,4,L,12,L,12,L,8,R,10,R,10,L,6,L,4,L,12,R,10,L,8,L,4,R,10,L,6,L,4,L,12,L,12,L,8,R,10,R,10,R,10,L,8,L,4,R,10,L,6,L,4,L,12,R,10,L,8,L,4,R,10 + // | | || + + let input = vec![ + "A,B,A,B,C,B,A,C,B,C\n", + "L,12,L,8,R,10,R,10\n", + "L,6,L,4,L,12\n", + "R,10,L,8,L,4,R,10\n", + "y\n", + ]; + program + .with_mem_0(Intcode::from(2)) + .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); + } + } +} |