summaryrefslogtreecommitdiff
path: root/src/bin/day_2.rs
diff options
context:
space:
mode:
authorJustin Wernick <justin@worthe-it.co.za>2019-12-02 22:52:37 +0200
committerJustin Wernick <justin@worthe-it.co.za>2019-12-02 22:52:37 +0200
commitefbf5a4ef9296ef0ae20876a002f7fd0570ad6e8 (patch)
treeca33de31e7c800952403cec9b8129981b6874397 /src/bin/day_2.rs
parent3988b89729bda5d43f36c2de65ad88000c6c6b02 (diff)
Removed redundant call to run program
Diffstat (limited to 'src/bin/day_2.rs')
-rw-r--r--src/bin/day_2.rs23
1 files changed, 15 insertions, 8 deletions
diff --git a/src/bin/day_2.rs b/src/bin/day_2.rs
index f1a1ac7..172a89a 100644
--- a/src/bin/day_2.rs
+++ b/src/bin/day_2.rs
@@ -115,10 +115,14 @@ impl Program {
}
fn execute(&self) -> Result<Intcode, ProgramError> {
- if self.run_to_termination().error {
+ self.run_to_termination().into_result()
+ }
+
+ fn into_result(&self) -> Result<Intcode, ProgramError> {
+ if self.error {
Err(ProgramError)
} else {
- Ok(self.run_to_termination().codes.head().unwrap().clone())
+ Ok(self.codes.head().unwrap().clone())
}
}
@@ -140,10 +144,10 @@ impl Program {
}
fn add(&self) -> Program {
- match (self.get_deref(1), self.get_dered(2), self.get(3)) {
+ 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),
+ codes: self.codes.update(out as usize, in1 + in2),
..self.clone()
},
_ => self.error(),
@@ -151,10 +155,10 @@ impl Program {
}
fn multiply(&self) -> Program {
- match (self.get_deref(1), self.get_dered(2), self.get(3)) {
+ 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),
+ codes: self.codes.update(out as usize, in1 * in2),
..self.clone()
},
_ => self.error(),
@@ -177,12 +181,15 @@ impl Program {
}
fn get(&self, pointer_offset: usize) -> Option<Intcode> {
- self.codes.get(self.program_counter + pointer_offset)
+ 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))
+ .and_then(|r| self.codes.get(r as usize))
+ .cloned()
}
}