summaryrefslogtreecommitdiff
path: root/src/bin/day_5.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_5.rs
parentad526abdd30e3495024b62e79f9fa0dc81cec613 (diff)
Intcode program into lib! It's shared code now.
Diffstat (limited to 'src/bin/day_5.rs')
-rw-r--r--src/bin/day_5.rs235
1 files changed, 3 insertions, 232 deletions
diff --git a/src/bin/day_5.rs b/src/bin/day_5.rs
index a3cf869..07f7af8 100644
--- a/src/bin/day_5.rs
+++ b/src/bin/day_5.rs
@@ -1,15 +1,9 @@
-use rpds::{list::List, 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 = i32;
-
#[derive(Debug, StructOpt)]
#[structopt(name = "Day 5: Sunny with a Chance of Asteroids")]
/// Executes an Intcode program
@@ -27,13 +21,13 @@ 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"))
.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::<Program>()
+ .collect::<IntcodeProgram>()
.with_input(opt.input.into_iter().collect());
let result = exit_on_failed_assertion(program.execute(), "Program errored");
@@ -49,226 +43,3 @@ fn exit_on_failed_assertion<A, E: std::error::Error>(data: Result<A, E>, message
}
}
}
-
-#[derive(Debug, Clone)]
-struct Program {
- instruction_pointer: usize,
- error: bool,
- halted: bool,
- memory: Vector<Intcode>,
- input: List<Intcode>,
- output: Vector<Intcode>,
-}
-
-impl FromIterator<Intcode> for Program {
- fn from_iter<I: IntoIterator<Item = Intcode>>(iter: I) -> Self {
- Program {
- instruction_pointer: 0,
- error: false,
- halted: false,
- memory: iter.into_iter().collect(),
- input: List::new(),
- output: Vector::new(),
- }
- }
-}
-
-impl Program {
- fn with_input(&self, input: List<Intcode>) -> Program {
- Program {
- input,
- ..self.clone()
- }
- }
-
- fn with_instruction_pointer(&self, instruction_pointer: usize) -> Program {
- Program {
- instruction_pointer,
- ..self.clone()
- }
- }
-
- fn with_instruction_pointer_offset(&self, offset: usize) -> Program {
- Program {
- instruction_pointer: self.instruction_pointer + offset,
- ..self.clone()
- }
- }
-
- fn with_memory_set(&self, address: usize, value: Intcode) -> Program {
- self.memory
- .set(address, value)
- .map(|memory| Program {
- memory,
- ..self.clone()
- })
- .unwrap_or(self.error())
- }
-
- fn with_input_consumed(&self) -> Program {
- self.input
- .drop_first()
- .map(|input| Program {
- input,
- ..self.clone()
- })
- .unwrap_or(self.error())
- }
-
- fn with_output(&self, print: Intcode) -> Program {
- Program {
- output: self.output.push_back(print),
- ..self.clone()
- }
- }
-
- fn execute(&self) -> Result<Vector<Intcode>, ProgramError> {
- self.run_to_termination().into_result()
- }
-
- fn into_result(&self) -> Result<Vector<Intcode>, ProgramError> {
- if self.error {
- Err(ProgramError)
- } else {
- Ok(self.output.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);
- self.memory
- .get(self.instruction_pointer)
- .map(|&opcode| match opcode % 100 {
- 1 => self.add(opcode),
- 2 => self.multiply(opcode),
- 3 => self.input(opcode),
- 4 => self.output(opcode),
- 5 => self.jump_if_true(opcode),
- 6 => self.jump_if_false(opcode),
- 7 => self.less_than(opcode),
- 8 => self.equals(opcode),
- 99 => self.halt(),
- _ => self.error(),
- })
- .unwrap_or(self.error())
- }
-
- fn add(&self, mode: Intcode) -> Program {
- match (self.get(1, mode), self.get(2, mode), self.get_immediate(3)) {
- (Some(in1), Some(in2), Some(out)) => self
- .with_instruction_pointer_offset(4)
- .with_memory_set(out as usize, in1 + in2),
- _ => self.error(),
- }
- }
-
- fn multiply(&self, mode: Intcode) -> Program {
- match (self.get(1, mode), self.get(2, mode), self.get_immediate(3)) {
- (Some(in1), Some(in2), Some(out)) => self
- .with_instruction_pointer_offset(4)
- .with_memory_set(out as usize, in1 * in2),
- _ => self.error(),
- }
- }
-
- fn input(&self, _mode: Intcode) -> Program {
- match (self.input.first().cloned(), self.get_immediate(1)) {
- (Some(input), Some(out)) => self
- .with_instruction_pointer_offset(2)
- .with_memory_set(out as usize, input)
- .with_input_consumed(),
- _ => self.error(),
- }
- }
-
- fn output(&self, mode: Intcode) -> Program {
- match self.get(1, mode) {
- Some(print) => self.with_instruction_pointer_offset(2).with_output(print),
- _ => self.error(),
- }
- }
-
- fn jump_if_true(&self, mode: Intcode) -> Program {
- match (self.get(1, mode), self.get(2, mode)) {
- (Some(pred), Some(to)) if pred != 0 => self.with_instruction_pointer(to as usize),
- (Some(_), Some(_)) => self.with_instruction_pointer_offset(3),
- _ => self.error(),
- }
- }
- fn jump_if_false(&self, mode: Intcode) -> Program {
- match (self.get(1, mode), self.get(2, mode)) {
- (Some(pred), Some(to)) if pred == 0 => self.with_instruction_pointer(to as usize),
- (Some(_), Some(_)) => self.with_instruction_pointer_offset(3),
- _ => self.error(),
- }
- }
-
- fn less_than(&self, mode: Intcode) -> Program {
- match (self.get(1, mode), self.get(2, mode), self.get_immediate(3)) {
- (Some(in1), Some(in2), Some(out)) => self
- .with_instruction_pointer_offset(4)
- .with_memory_set(out as usize, if in1 < in2 { 1 } else { 0 }),
- _ => self.error(),
- }
- }
-
- fn equals(&self, mode: Intcode) -> Program {
- match (self.get(1, mode), self.get(2, mode), self.get_immediate(3)) {
- (Some(in1), Some(in2), Some(out)) => self
- .with_instruction_pointer_offset(4)
- .with_memory_set(out as usize, if in1 == in2 { 1 } else { 0 }),
- _ => 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, mode: Intcode) -> Option<Intcode> {
- match mode / (10 as Intcode).pow(pointer_offset as u32 + 1) % 10 {
- 0 => self.get_position(pointer_offset),
- 1 => self.get_immediate(pointer_offset),
- _ => None,
- }
- }
-
- fn get_immediate(&self, pointer_offset: usize) -> Option<Intcode> {
- self.memory
- .get(self.instruction_pointer + pointer_offset)
- .cloned()
- }
-
- fn get_position(&self, pointer_offset: usize) -> Option<Intcode> {
- self.get_immediate(pointer_offset)
- .and_then(|r| self.memory.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")
- }
-}
-impl std::error::Error for ProgramError {}