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_25.rs | 110 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 2019/src/bin/day_25.rs (limited to '2019/src/bin/day_25.rs') diff --git a/2019/src/bin/day_25.rs b/2019/src/bin/day_25.rs new file mode 100644 index 0000000..522789e --- /dev/null +++ b/2019/src/bin/day_25.rs @@ -0,0 +1,110 @@ +use aoc2019::*; +use std::io; +use std::io::prelude::*; +use std::process; +use structopt::StructOpt; + +#[derive(Debug, StructOpt)] +#[structopt(name = "Day 25: Cryostasis")] +/// Pilots a robot to save Santa! +/// +/// See https://adventofcode.com/2019/day/25 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::(), "Invalid number")) + .collect::(); + + let input = vec![ + "east", + "take antenna", + "west", + "north", + "take weather machine", + "north", + "take klein bottle", + "east", + "take spool of cat6", + "east", + "south", + "take mug", + "north", + "north", + "west", + "north", + "take cake", + "south", + "east", + "east", + "north", + "north", + "take tambourine", + "south", + "south", + "south", + "take shell", + "north", + "west", + "south", + "west", + "south", + "south", + "inv", + //"drop mug", + //"drop weather machine", + "drop cake", + "drop shell", + "drop klein bottle", + "drop tambourine", + //"drop antenna", + //"drop spool of cat6", + "east", + ]; + + let result = exit_on_failed_assertion( + program + .with_input( + input + .iter() + .flat_map(|line| { + line.chars() + .map(|c| Intcode::from(c as u8)) + .chain(vec![Intcode::from(10)].into_iter()) + }) + .collect(), + ) + .run_to_termination_or_input() + .output_into_result(), + "Program failed", + ); + + println!( + "{}", + result + .drop_last() + .unwrap() + .iter() + .flat_map(|c| c.to_signed_bytes_be()) + .map(|c| c as char) + .collect::() + ); + println!("{}", result.last().unwrap()); +} + +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