[2016][rust][12.0] Screw immutability

profile
Alpha Chen 8 years ago
parent 01cfb8261c
commit 020f34f3e5

@ -1,22 +1,119 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::str;
use errors::*; use errors::*;
pub fn solve(input: &str) -> Result<String> { pub fn solve(input: &str) -> Result<String> {
Ok("".into()) let instructions: Instructions = input.parse()?;
let assembunny = Assembunny::new(instructions);
let registers = assembunny.last().ok_or("")?;
Ok(format!("{}", registers.get(Register::A)))
}
struct Assembunny {
registers: Registers,
instructions: Instructions,
}
impl Assembunny {
fn new(instructions: Instructions) -> Self {
Assembunny {
registers: Registers::new(),
instructions: instructions,
}
}
fn value<V: Into<Variable>>(&self, v: V) -> isize {
let v: Variable = v.into();
match v {
Variable::Register(r) => self.registers.get(r),
Variable::Value(i) => i,
}
}
}
impl Iterator for Assembunny {
type Item = Registers;
fn next(&mut self) -> Option<Registers> {
let pc = self.value(Register::PC) as usize;
let instruction = match self.instructions.0.get(pc) {
Some(i) => i,
None => {
return None;
}
};
match *instruction {
Instruction::Cpy(v, r) => {
let value = self.value(v);
self.registers.set(r, value);
self.registers.inc(Register::PC);
}
Instruction::Inc(r) => {
self.registers.inc(r);
self.registers.inc(Register::PC);
}
Instruction::Dec(r) => {
self.registers.dec(r);
self.registers.inc(Register::PC);
}
Instruction::Jnz(v, i) => {
let delta = if self.value(v) == 0 { 1 } else { i };
let pc = self.value(Register::PC) + delta;
self.registers.set(Register::PC, pc);
}
}
Some(self.registers.clone())
}
}
#[derive(Clone, Debug)]
struct Registers(HashMap<Register, isize>);
struct Instructions(Vec<Instruction>);
impl Registers {
fn new() -> Self {
Registers(HashMap::new())
}
fn get(&self, r: Register) -> isize {
self.0.get(&r).cloned().unwrap_or(0)
}
fn set(&mut self, r: Register, i: isize) {
self.0.insert(r, i);
}
fn inc(&mut self, r: Register) {
let v = self.get(r) + 1;
self.set(r, v);
}
fn dec(&mut self, r: Register) {
let v = self.get(r) - 1;
self.set(r, v);
}
} }
// 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:
#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)] #[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
enum Register { enum Register {
PC,
A, A,
B, B,
C, C,
D, D,
} }
#[derive(Clone, Copy)] #[derive(Clone, Copy, Debug, PartialEq)]
enum Instruction {
Cpy(Variable, Register),
Inc(Register),
Dec(Register),
Jnz(Variable, isize),
}
#[derive(Clone, Copy, Debug, PartialEq)]
enum Variable { enum Variable {
Register(Register), Register(Register),
Value(isize), Value(isize),
@ -28,148 +125,153 @@ impl From<Register> for Variable {
} }
} }
impl From<isize> for Variable { // Parsing
fn from(i: isize) -> Self {
Variable::Value(i) impl str::FromStr for Instructions {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
s.lines()
.map(|line| line.parse())
.collect::<Result<Vec<_>>>()
.map(Instructions)
} }
} }
#[derive(Debug, PartialEq)] impl str::FromStr for Register {
struct State { type Err = Error;
pc: usize, fn from_str(s: &str) -> Result<Self> {
registers: HashMap<Register, isize>, match s {
"a" => Ok(Register::A),
"b" => Ok(Register::B),
"c" => Ok(Register::C),
"d" => Ok(Register::D),
_ => Err(format!("invalid register '{}'", s).into()),
} }
impl State {
fn value(&self, v: Variable) -> isize {
match v {
Variable::Register(ref r) => {
self.registers.get(r).cloned().unwrap_or(0 as isize)
} }
Variable::Value(value) => value,
} }
impl str::FromStr for Instruction {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
let mut tokens = s.split_whitespace();
match tokens.next() {
Some("cpy") => {
let v = tokens.read_variable()?;
let r = tokens.read_register()?;
Ok(Instruction::Cpy(v, r))
} }
Some("inc") => {
let r = tokens.read_register()?;
Ok(Instruction::Inc(r))
} }
Some("dec") => {
// cpy x y copies x (either an integer or the value of a register) into register y. let r = tokens.read_register()?;
struct Copy { Ok(Instruction::Dec(r))
variable: Variable,
register: Register,
} }
Some("jnz") => {
impl Copy { let var = tokens.read_variable()?;
fn run(&self, state: &State) -> State { let val = tokens.read_value()?;
let pc = state.pc + 1; Ok(Instruction::Jnz(var, val))
let mut registers = state.registers.clone();
let value = state.value(self.variable);
registers.insert(self.register, value);
State {
pc: pc,
registers: registers,
} }
Some(inst) => Err(format!("invalid instruction '{}'", inst).into()),
None => Err("no instruction".into()),
} }
} }
// inc x increases the value of register x by one.
struct Increment {
register: Register,
} }
impl Increment { impl str::FromStr for Variable {
fn run(&self, state: &State) -> State { type Err = Error;
let pc = state.pc + 1; fn from_str(s: &str) -> Result<Self> {
let mut registers = state.registers.clone(); s.parse::<Register>()
let value = state.value(self.register.into()) + 1; .map(Variable::Register)
registers.insert(self.register, value); .or_else(|_| s.parse::<isize>().map(Variable::Value))
State { .map_err(|_| format!("invalid variable '{}'", s).into())
pc: pc,
registers: registers,
} }
} }
trait SplitWhitespaceExt {
fn read_variable(&mut self) -> Result<Variable>;
fn read_register(&mut self) -> Result<Register>;
fn read_value(&mut self) -> Result<isize>;
} }
// dec x decreases the value of register x by one. impl<'a> SplitWhitespaceExt for str::SplitWhitespace<'a> {
struct Decrement { fn read_variable(&mut self) -> Result<Variable> {
register: Register, self.next()
.ok_or("missing variable".into())
.and_then(|v| v.parse::<Variable>())
} }
impl Decrement { fn read_register(&mut self) -> Result<Register> {
fn run(&self, state: &State) -> State { self.next()
let pc = state.pc + 1; .ok_or("missing register".into())
let mut registers = state.registers.clone(); .and_then(|v| v.parse::<Register>())
let value = state.value(self.register.into()) - 1;
registers.insert(self.register, value);
State {
pc: pc,
registers: registers,
} }
fn read_value(&mut self) -> Result<isize> {
self.next()
.ok_or("missing value".into())
.and_then(|v| v.parse::<isize>().chain_err(|| ""))
} }
} }
// jnz x y jumps to an instruction y away (positive means forward; negative means backward), but only if x is not zero.
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::{Variable, Register, State, Copy, Increment, Decrement}; use super::{Assembunny, Instructions, Instruction, Register, Variable};
use std::str::FromStr;
#[test] #[test]
fn test_state_value() { fn test_assembunny() {
let state = State { let instructions: Instructions = "cpy 41 a
pc: 0, inc a
registers: vec![(Register::A, 41)].into_iter().collect(), inc a
}; dec a
assert_eq!(state.value(Register::A.into()), 41); jnz a 2
assert_eq!(state.value(Register::B.into()), 0); dec a"
assert_eq!(state.value(23.into()), 23); .parse()
} .unwrap();
let mut assembunny = Assembunny::new(instructions);
#[test] let registers = assembunny.next().unwrap();
fn test_copy() { assert_eq!(registers.get(Register::A), 41);
let variable = Variable::Value(41); assert_eq!(registers.get(Register::B), 0);
let register = Register::A;
let copy = Copy { let registers = assembunny.next().unwrap();
variable: variable, assert_eq!(registers.get(Register::A), 42);
register: register, assert_eq!(registers.get(Register::C), 0);
};
let state = State {
pc: 0,
registers: vec![].into_iter().collect(),
};
let expected = State {
pc: 1,
registers: vec![(Register::A, 41)].into_iter().collect(),
};
assert_eq!(copy.run(&state), expected); let registers = assembunny.last().unwrap();
assert_eq!(registers.get(Register::A), 42);
assert_eq!(registers.get(Register::PC), 6);
} }
#[test] #[test]
fn test_increment() { fn test_instructions_from_str() {
let increment = Increment { register: Register::A }; let i: Instructions = "cpy 41 a
let state = State { inc a
pc: 0, inc a
registers: vec![(Register::A, 41)].into_iter().collect(), dec a
}; jnz a 2
let expected = State { dec a"
pc: 1, .parse()
registers: vec![(Register::A, 42)].into_iter().collect(), .unwrap();
}; assert_eq!(i.0.len(), 6);
assert_eq!(i.0[0], Instruction::from_str("cpy 41 a").unwrap());
assert_eq!(increment.run(&state), expected);
} }
#[test] #[test]
fn test_decrement() { fn test_instruction_from_str() {
let decrement = Decrement { register: Register::A }; assert!(Instruction::from_str("").is_err());
let state = State { assert!(Instruction::from_str("omg").is_err());
pc: 0, assert!(Instruction::from_str("inc 5").is_err());
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); assert_eq!(Instruction::from_str("cpy 41 a").unwrap(),
Instruction::Cpy(Variable::Value(41), Register::A));
assert_eq!(Instruction::from_str("inc a").unwrap(),
Instruction::Inc(Register::A));
assert_eq!(Instruction::from_str("dec b").unwrap(),
Instruction::Dec(Register::B));
assert_eq!(Instruction::from_str("jnz c 2").unwrap(),
Instruction::Jnz(Variable::Register(Register::C), 2));
} }
} }

Loading…
Cancel
Save