[2016][rust][12.0] Clone/Copy

profile
Alpha Chen 8 years ago
parent b457ce5027
commit 2585163655

@ -8,7 +8,7 @@ pub fn solve(input: &str) -> Result<String> {
// The assembunny code you've extracted operates on four registers (a, b, c, and d) that start at 0 // The assembunny code you've extracted operates on four registers (a, b, c, and d) that start at 0
// and can hold any integer. However, it seems to make use of only a few instructions: // and can hold any integer. However, it seems to make use of only a few instructions:
#[derive(Clone, Debug, Hash, Eq, PartialEq)] #[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
enum Register { enum Register {
A, A,
B, B,
@ -16,11 +16,24 @@ enum Register {
D, D,
} }
#[derive(Clone, Copy)]
enum Variable { enum Variable {
Register(Register), Register(Register),
Value(isize), Value(isize),
} }
impl From<Register> for Variable {
fn from(r: Register) -> Self {
Variable::Register(r)
}
}
impl From<isize> for Variable {
fn from(i: isize) -> Self {
Variable::Value(i)
}
}
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
struct State { struct State {
pc: usize, pc: usize,
@ -28,12 +41,12 @@ struct State {
} }
impl State { impl State {
fn value(&self, v: &Variable) -> isize { fn value(&self, v: Variable) -> isize {
match v { match v {
&Variable::Register(ref r) => { Variable::Register(ref r) => {
self.registers.get(r).cloned().unwrap_or(0 as isize) self.registers.get(r).cloned().unwrap_or(0 as isize)
} }
&Variable::Value(value) => value, Variable::Value(value) => value,
} }
} }
} }
@ -47,9 +60,9 @@ struct Copy {
impl Copy { impl Copy {
fn run(&self, state: &State) -> State { fn run(&self, state: &State) -> State {
let pc = state.pc + 1; let pc = state.pc + 1;
let value = state.value(&self.variable);
let mut registers = state.registers.clone(); let mut registers = state.registers.clone();
registers.insert(self.register.clone(), value); let value = state.value(self.variable);
registers.insert(self.register, value);
State { State {
pc: pc, pc: pc,
registers: registers, registers: registers,
@ -66,9 +79,8 @@ impl Increment {
fn run(&self, state: &State) -> State { fn run(&self, state: &State) -> State {
let pc = state.pc + 1; let pc = state.pc + 1;
let mut registers = state.registers.clone(); let mut registers = state.registers.clone();
let register = self.register.clone(); let value = state.value(self.register.into()) + 1;
let value = state.value(&Variable::Register(self.register.clone())) + 1; registers.insert(self.register, value);
registers.insert(register, value);
State { State {
pc: pc, pc: pc,
registers: registers, registers: registers,
@ -89,9 +101,9 @@ mod tests {
pc: 0, pc: 0,
registers: vec![(Register::A, 41)].into_iter().collect(), registers: vec![(Register::A, 41)].into_iter().collect(),
}; };
assert_eq!(state.value(&Variable::Register(Register::A)), 41); assert_eq!(state.value(Register::A.into()), 41);
assert_eq!(state.value(&Variable::Register(Register::B)), 0); assert_eq!(state.value(Register::B.into()), 0);
assert_eq!(state.value(&Variable::Value(23)), 23); assert_eq!(state.value(23.into()), 23);
} }
#[test] #[test]

Loading…
Cancel
Save