Created
December 16, 2016 20:56
-
-
Save anonymous/b2b8d8149b790bef4a1fc937b2fb41e9 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(try_from)] | |
use std::convert::TryFrom; | |
fn do_add<'a>(state : &mut std::collections::HashMap<&'a str, i32>, thing_one : &'a str, thing_two : &'a str){ | |
let one = state.get(thing_one).unwrap().clone(); | |
let two = state.get(thing_two).unwrap().clone(); | |
state.insert(thing_one, 0); | |
state.insert(thing_two, one + two); | |
} | |
fn do_copy<'a>(state : &mut std::collections::HashMap<&'a str, i32>, thing_one : &str, thing_two : &'a str){ | |
match thing_one.parse::<i32>() { | |
Ok(value) => { | |
state.insert(thing_two, value); | |
} | |
_ => { | |
let value = state.get(thing_one).unwrap().clone(); | |
state.insert(thing_two, value); | |
} | |
} | |
return | |
} | |
fn do_inc<'a>(state : &mut std::collections::HashMap<&'a str, i32>, thing : &'a str){ | |
let previous = state.get(thing).unwrap().clone(); | |
state.insert(thing, previous + 1); | |
return | |
} | |
fn do_dec<'a>(state : &mut std::collections::HashMap<&'a str, i32>, thing : &'a str){ | |
let previous = state.get(thing).unwrap().clone(); | |
state.insert(thing, previous - 1); | |
return | |
} | |
fn do_jump(state : &mut std::collections::HashMap<&str, i32>, pc : usize, thing_one : &str, thing_two : &str) -> usize { | |
let x = match thing_one.parse::<i32>() { | |
Ok(value) => { | |
value | |
} | |
_ => { | |
state.get(thing_one).unwrap().clone() | |
} | |
}; | |
let y = match thing_two.parse::<i32>() { | |
Ok(value) => { | |
value | |
} | |
_ => { | |
state.get(thing_one).unwrap().clone() | |
} | |
}; | |
if x != 0 { | |
return usize::try_from(i32::try_from(pc).unwrap() + y).unwrap() | |
} | |
return pc + 1 | |
} | |
fn main () { | |
let input = | |
"cpy 1 a // 0 a = 1 | |
cpy 1 b // 1 b = 1 | |
cpy 26 d // 2 d = 26 | |
jnz c 2 // 3 | |
jnz 1 5 // 4 => 9 | |
cpy 7 c // 5 | |
inc d // 6 | |
dec c // 7 | |
jnz c -2 // 8 | |
cpy a c // 9 { c = a | |
nop //10 { a++ | |
nop //11 b-- | |
add b a //12 } => a += b ; b = 0 | |
cpy c b //13 b = c | |
dec d //14 d -- | |
jnz d -6 //15 } c = a; while d--: a += b; b = c; c = a => ? | |
cpy 19 c //16 c = 16 | |
cpy 11 d //17 d = 11 | |
nop //18 | |
nop //19 | |
add d a //20 | |
dec c //21 | |
jnz c -5 //22"; | |
let commands : Vec<&str> = input.split("\n").collect(); | |
let mut pc = 0; | |
let mut state = std::collections::HashMap::<&str, i32>::new(); | |
state.insert(&"a", 0); | |
state.insert(&"b", 0); | |
state.insert(&"c", 0); | |
state.insert(&"d", 0); | |
while pc < commands.len() { | |
println!("{:?} {:?} {:?}", pc, commands[pc], state); | |
let split : Vec<&str> = commands[pc].split(" ").collect(); | |
match split[0] { | |
"nop" => {pc = pc + 1;} | |
"add" => {do_add(&mut state, split[1], split[2]); pc = pc + 1;} | |
"cpy" => {do_copy(&mut state, split[1], split[2]); pc = pc + 1;} | |
"inc" => {do_inc(&mut state, split[1]); pc = pc + 1;} | |
"dec" => {do_dec(&mut state, split[1]); pc = pc + 1;} | |
"jnz" => {pc = do_jump(&mut state, pc, split[1], split[2]);} | |
_ => {println!("WTF: {:?}", split[0]);} | |
} | |
} | |
println!("{:?}", state.get(&"a").unwrap()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment