summaryrefslogtreecommitdiff
path: root/src/bin/day_2.rs
diff options
context:
space:
mode:
authorJustin Wernick <justin@worthe-it.co.za>2019-12-11 23:02:42 +0200
committerJustin Wernick <justin@worthe-it.co.za>2019-12-11 23:02:42 +0200
commit003a62f1c38344c5a647170bd2472c4eab39cf75 (patch)
tree1e32598b23b10d5ab9be1b8c65f6e3493f29b3d1 /src/bin/day_2.rs
parentad526abdd30e3495024b62e79f9fa0dc81cec613 (diff)
Intcode program into lib! It's shared code now.
Diffstat (limited to 'src/bin/day_2.rs')
-rw-r--r--src/bin/day_2.rs155
1 files changed, 22 insertions, 133 deletions
diff --git a/src/bin/day_2.rs b/src/bin/day_2.rs
index 172a89a..ce8fcaa 100644
--- a/src/bin/day_2.rs
+++ b/src/bin/day_2.rs
@@ -1,15 +1,9 @@
-use im::vector::Vector;
-use std::fmt;
+use aoc2019::*;
use std::io;
use std::io::prelude::*;
-use std::iter;
-use std::iter::FromIterator;
-use std::iter::IntoIterator;
use std::process;
use structopt::StructOpt;
-type Intcode = u32;
-
#[derive(Debug, StructOpt)]
#[structopt(name = "Day 2: 1202 Program Alarm")]
/// Executes an Intcode program
@@ -36,7 +30,7 @@ fn main() {
let stdin = io::stdin();
let opt = Opt::from_args();
- let program: Program = stdin
+ let program: IntcodeProgram = stdin
.lock()
.split(b',')
.map(|x| exit_on_failed_assertion(x, "Error reading input"))
@@ -47,14 +41,14 @@ fn main() {
match (opt.noun, opt.verb, opt.output) {
(Some(noun), Some(verb), _) => {
let result = exit_on_failed_assertion(
- program.set_input(noun, verb).execute(),
+ program.with_noun_verb_input(noun, verb).execute(),
"Program errored",
);
println!("{}", result);
}
(_, _, Some(output)) => {
let (noun, verb) =
- exit_on_failed_assertion(program.find_input(output), "Program errored");
+ exit_on_failed_assertion(find_input(&program, output), "Program errored");
println!("({}, {})", noun, verb);
}
(None, None, None) => {
@@ -78,127 +72,22 @@ fn exit_on_failed_assertion<A, E: std::error::Error>(data: Result<A, E>, message
}
}
-#[derive(Debug, Clone)]
-struct Program {
- program_counter: usize,
- error: bool,
- halted: bool,
- codes: Vector<Intcode>,
-}
-
-impl FromIterator<Intcode> for Program {
- fn from_iter<I: IntoIterator<Item = Intcode>>(iter: I) -> Self {
- Program {
- program_counter: 0,
- error: false,
- halted: false,
- codes: iter.into_iter().collect(),
- }
- }
-}
-
-impl Program {
- fn set_input(&self, noun: Intcode, verb: Intcode) -> Program {
- Program {
- codes: self.codes.update(1, noun).update(2, verb),
- ..self.clone()
- }
- }
-
- fn find_input(&self, output: Intcode) -> Result<(Intcode, Intcode), ProgramError> {
- (0..99)
- .flat_map(|noun| (0..99).map(move |verb| (noun, verb)))
- .map(|(noun, verb)| (noun, verb, self.set_input(noun, verb).execute()))
- .find(|(_noun, _verb, out)| *out == Ok(output))
- .map(|(noun, verb, _out)| Ok((noun, verb)))
- .unwrap_or(Err(ProgramError))
- }
-
- fn execute(&self) -> Result<Intcode, ProgramError> {
- self.run_to_termination().into_result()
- }
-
- fn into_result(&self) -> Result<Intcode, ProgramError> {
- if self.error {
- Err(ProgramError)
- } else {
- Ok(self.codes.head().unwrap().clone())
- }
- }
-
- fn run_to_termination(&self) -> Program {
- iter::successors(Some(self.clone()), |p| Some(Program::next(&p)))
- .find(|p| p.halted)
- .unwrap() // successors doesn't terminate, so this will never be none.
- }
-
- fn next(&self) -> Program {
- //eprintln!("{:?}", self);
- match self.codes.get(self.program_counter) {
- Some(1) => self.add(),
- Some(2) => self.multiply(),
- Some(99) => self.halt(),
- Some(_) => self.error(),
- None => self.error(),
- }
- }
-
- fn add(&self) -> Program {
- match (self.get_deref(1), self.get_deref(2), self.get(3)) {
- (Some(in1), Some(in2), Some(out)) => Program {
- program_counter: self.program_counter + 4,
- codes: self.codes.update(out as usize, in1 + in2),
- ..self.clone()
- },
- _ => self.error(),
- }
- }
-
- fn multiply(&self) -> Program {
- match (self.get_deref(1), self.get_deref(2), self.get(3)) {
- (Some(in1), Some(in2), Some(out)) => Program {
- program_counter: self.program_counter + 4,
- codes: self.codes.update(out as usize, in1 * in2),
- ..self.clone()
- },
- _ => self.error(),
- }
- }
-
- fn halt(&self) -> Program {
- Program {
- halted: true,
- ..self.clone()
- }
- }
-
- fn error(&self) -> Program {
- Program {
- halted: true,
- error: true,
- ..self.clone()
- }
- }
-
- fn get(&self, pointer_offset: usize) -> Option<Intcode> {
- self.codes
- .get(self.program_counter + pointer_offset)
- .cloned()
- }
-
- fn get_deref(&self, pointer_offset: usize) -> Option<Intcode> {
- self.get(pointer_offset)
- .and_then(|r| self.codes.get(r as usize))
- .cloned()
- }
-}
-
-#[derive(Debug, PartialEq)]
-struct ProgramError;
-
-impl fmt::Display for ProgramError {
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- write!(f, "Unknown error")
- }
+fn find_input(
+ program: &IntcodeProgram,
+ output: Intcode,
+) -> Result<(Intcode, Intcode), IntcodeProgramError> {
+ (0..99)
+ .flat_map(|noun| (0..99).map(move |verb| (noun, verb)))
+ .map(|(noun, verb)| {
+ (
+ noun,
+ verb,
+ program
+ .with_noun_verb_input(noun, verb)
+ .execute_returning_memory_0(),
+ )
+ })
+ .find(|(_noun, _verb, out)| *out == Ok(Some(output)))
+ .map(|(noun, verb, _out)| Ok((noun, verb)))
+ .unwrap_or(Err(IntcodeProgramError))
}
-impl std::error::Error for ProgramError {}