From 48a9be141afadaa24182781a2db58594cb2ce522 Mon Sep 17 00:00:00 2001 From: Justin Wernick Date: Thu, 23 Jan 2020 00:49:39 +0200 Subject: Removed redundant match statements left over from an old implementation The old version used to have error handling for out of range memory access. --- src/lib.rs | 96 +++++++++++++++++++++++--------------------------------------- 1 file changed, 36 insertions(+), 60 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 08082d8..a7dfc02 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -198,98 +198,74 @@ impl IntcodeProgram { } fn add(&self, mode: &Intcode) -> IntcodeProgram { - match ( - self.get(1, mode), - self.get(2, mode), + self.with_instruction_pointer_offset(4).with_memory_set( self.get_literal(3, mode), - ) { - (in1, in2, out) => self - .with_instruction_pointer_offset(4) - .with_memory_set(out, in1 + in2), - } + self.get(1, mode) + self.get(2, mode), + ) } fn multiply(&self, mode: &Intcode) -> IntcodeProgram { - match ( - self.get(1, mode), - self.get(2, mode), + self.with_instruction_pointer_offset(4).with_memory_set( self.get_literal(3, mode), - ) { - (in1, in2, out) => self - .with_instruction_pointer_offset(4) - .with_memory_set(out, in1 * in2), - } + self.get(1, mode) * self.get(2, mode), + ) } fn input(&self, mode: &Intcode) -> IntcodeProgram { - match (self.input.first().cloned(), self.get_literal(1, mode)) { - (Some(input), out) => self + match self.input.first().cloned() { + Some(input) => self .with_instruction_pointer_offset(2) - .with_memory_set(out, input) + .with_memory_set(self.get_literal(1, mode), input) .with_input_consumed(), - (None, _out) => self.await_input(), + None => self.await_input(), } } fn output(&self, mode: &Intcode) -> IntcodeProgram { - match self.get(1, mode) { - print => self.with_instruction_pointer_offset(2).with_output(print), - } + self.with_instruction_pointer_offset(2) + .with_output(self.get(1, mode)) } fn jump_if_true(&self, mode: &Intcode) -> IntcodeProgram { - match (self.get(1, mode), self.get(2, mode)) { - (ref pred, ref to) if !pred.is_zero() => self.with_instruction_pointer(to.clone()), - (_, _) => self.with_instruction_pointer_offset(3), + if !self.get(1, mode).is_zero() { + self.with_instruction_pointer(self.get(2, mode)) + } else { + self.with_instruction_pointer_offset(3) } } fn jump_if_false(&self, mode: &Intcode) -> IntcodeProgram { - match (self.get(1, mode), self.get(2, mode)) { - (ref pred, ref to) if pred.is_zero() => self.with_instruction_pointer(to.clone()), - (_, _) => self.with_instruction_pointer_offset(3), + if self.get(1, mode).is_zero() { + self.with_instruction_pointer(self.get(2, mode)) + } else { + self.with_instruction_pointer_offset(3) } } fn less_than(&self, mode: &Intcode) -> IntcodeProgram { - match ( - self.get(1, mode), - self.get(2, mode), + self.with_instruction_pointer_offset(4).with_memory_set( self.get_literal(3, mode), - ) { - (in1, in2, out) => self.with_instruction_pointer_offset(4).with_memory_set( - out, - if in1 < in2 { - Intcode::from(1) - } else { - Intcode::from(0) - }, - ), - } + if self.get(1, mode) < self.get(2, mode) { + Intcode::from(1) + } else { + Intcode::from(0) + }, + ) } fn equals(&self, mode: &Intcode) -> IntcodeProgram { - match ( - self.get(1, mode), - self.get(2, mode), + self.with_instruction_pointer_offset(4).with_memory_set( self.get_literal(3, mode), - ) { - (in1, in2, out) => self.with_instruction_pointer_offset(4).with_memory_set( - out, - if in1 == in2 { - Intcode::from(1) - } else { - Intcode::from(0) - }, - ), - } + if self.get(1, mode) == self.get(2, mode) { + Intcode::from(1) + } else { + Intcode::from(0) + }, + ) } fn set_relative_base(&self, mode: &Intcode) -> IntcodeProgram { - match self.get(1, mode) { - base_change => self - .with_instruction_pointer_offset(2) - .with_relative_base(self.relative_base.clone() + base_change), - } + self.with_instruction_pointer_offset(2) + .with_relative_base(self.relative_base.clone() + self.get(1, mode)) } fn halt(&self) -> IntcodeProgram { -- cgit v1.2.3