summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Wernick <justin@worthe-it.co.za>2020-01-23 00:49:39 +0200
committerJustin Wernick <justin@worthe-it.co.za>2020-01-23 00:49:39 +0200
commit48a9be141afadaa24182781a2db58594cb2ce522 (patch)
treec8cabf69e2180fffe222d0b55a6bdbdbb367f1a1
parentcd651c866df78df8f050dcca1698d244ab0baee5 (diff)
Removed redundant match statements left over from an old implementation
The old version used to have error handling for out of range memory access.
-rw-r--r--src/lib.rs96
1 files changed, 36 insertions, 60 deletions
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 {