diff options
author | Justin Wernick <justin@worthe-it.co.za> | 2020-01-13 23:08:19 +0200 |
---|---|---|
committer | Justin Wernick <justin@worthe-it.co.za> | 2020-01-13 23:08:19 +0200 |
commit | fa3b0d8cb573a2a0700fa565d6cf12abf66dc70f (patch) | |
tree | 773f4f85edd2eba1dd3729d0f55e0f00b42f04eb /src/bin | |
parent | 87572a858c724f1c7414743ad397e79101c46727 (diff) |
Day 25: Text adventure game!
Diffstat (limited to 'src/bin')
-rw-r--r-- | src/bin/day_25.dot | 43 | ||||
-rw-r--r-- | src/bin/day_25.rs | 110 |
2 files changed, 153 insertions, 0 deletions
diff --git a/src/bin/day_25.dot b/src/bin/day_25.dot new file mode 100644 index 0000000..58dc131 --- /dev/null +++ b/src/bin/day_25.dot @@ -0,0 +1,43 @@ +digraph { + Breach [label="Hull Breach"] + Crew [label="Crew Quarters (antenna)"] + Hallway [label="Hallway (weather machine)"] + Storage [label="Storage (klein bottle)"] + Stables [label="Stables (spool of cat6)"] + Warp [label="Warp Drive Maintenance"] + Security [label="Security Checkpoint"] + Pressure [label="Pressure-Sensitive Floor"] + Gift [label="Gift Wrapping Center"] + Sick [label="Sick Bay (infinite loop X)"] + Chocolate [label="Hot Chocolate Fountain (giant electromagnet X)"] + Observatory [label="Observatory (cake)"] + Navigation [label="Navigation (escape pod XX)"] + Corridor [label="Corridor"] + Holodeck [label="Holodeck (molten lava X)"] + Science [label="Science Lab (tambourine)"] + Passages [label="Passages (shell)"] + Engineering [label="Engineering"] + Arcade [label="Arcade (photons X)"] + Kitchen [label="Kitchen (mug)"] + + + Breach -> Crew [label=East] + Breach -> Hallway [label=North] + Hallway -> Storage [label=North] + Storage -> Stables [label=East] + Stables -> Warp [label=South] + Warp -> Security [label=South] + Security -> Pressure [label=East] + Stables -> Gift [label=East] + Gift -> Sick [label=North] + Sick -> Chocolate [label=West] + Chocolate -> Observatory [label=North] + Chocolate -> Navigation [label=West] + Corridor -> Holodeck [label=North] + Holodeck -> Science [label=North] + Sick -> Corridor [label=East] + Corridor -> Passages [label=South] + Passages -> Engineering [label=East] + Engineering -> Arcade [label=South] + Gift -> Kitchen [label=South] +} diff --git a/src/bin/day_25.rs b/src/bin/day_25.rs new file mode 100644 index 0000000..5c132cd --- /dev/null +++ b/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::<Intcode>(), "Invalid number")) + .collect::<IntcodeProgram>(); + + 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::<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); + } + } +} |