From 01cfb8261c5444dd41e76f2b4e456ad07491f14d Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Tue, 3 Jan 2017 21:07:44 -0800 Subject: [PATCH] [2016][rust][12.0] Decrement --- 2016/rust/src/day_12.rs | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/2016/rust/src/day_12.rs b/2016/rust/src/day_12.rs index 92ff01b..13fb643 100644 --- a/2016/rust/src/day_12.rs +++ b/2016/rust/src/day_12.rs @@ -89,11 +89,28 @@ impl Increment { } // dec x decreases the value of register x by one. +struct Decrement { + register: Register, +} + +impl Decrement { + fn run(&self, state: &State) -> State { + let pc = state.pc + 1; + let mut registers = state.registers.clone(); + let value = state.value(self.register.into()) - 1; + registers.insert(self.register, value); + State { + pc: pc, + registers: registers, + } + } +} + // jnz x y jumps to an instruction y away (positive means forward; negative means backward), but only if x is not zero. #[cfg(test)] mod tests { - use super::{Variable, Register, State, Copy, Increment}; + use super::{Variable, Register, State, Copy, Increment, Decrement}; #[test] fn test_state_value() { @@ -140,4 +157,19 @@ mod tests { assert_eq!(increment.run(&state), expected); } + + #[test] + fn test_decrement() { + let decrement = Decrement { register: Register::A }; + let state = State { + pc: 0, + registers: vec![(Register::A, 41)].into_iter().collect(), + }; + let expected = State { + pc: 1, + registers: vec![(Register::A, 40)].into_iter().collect(), + }; + + assert_eq!(decrement.run(&state), expected); + } }