Skip to content

Instantly share code, notes, and snippets.

Created December 26, 2016 00:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/7e18f6589abe2eff510d412a79e7a186 to your computer and use it in GitHub Desktop.
Save anonymous/7e18f6589abe2eff510d412a79e7a186 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
#![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_out<'a>(state : &mut std::collections::HashMap<&'a str, i32>, thing : &'a str){
let previous = state.get(thing).unwrap().clone();
println!("{:?} = {:?}", thing, previous);
if previous != 0 && previous != 1 {
panic!();
}
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 a d
cpy 4 c { d = a + 4 * 633 ; b = 0; c = 0;
cpy 633 b
nop
nop
add b d
dec c
jnz c -5 }
cpy d a // from a == 0
nop // from a != 0
cpy a b {b = a % 2; a = a / 2 ; c = 0 {c = 2 - (a % 2); a = a/2 ; b = 0
cpy 0 a
cpy 2 c :X
jnz b 2 :Y {while b
jnz 1 6 = GOTO :OUT
dec b b--
dec c c--
jnz c -4 = GOTO :Y if c == 0 { a++; c = 2; }
inc a
jnz 1 -7 = GOTO :X }}
cpy 2 b :OUT { b = 2 - c ; c = 0
jnz c 2
jnz 1 4
dec b
dec c
jnz 1 -4 }}
nop
out b
jnz a -19
jnz 1 -21
";
let commands : Vec<&str> = input.split("\n").collect();
let mut pc = 0;
let mut state = std::collections::HashMap::<&str, i32>::new();
state.insert(&"a", 1);
state.insert(&"b", 0);
state.insert(&"c", 0);
state.insert(&"d", 0);
while pc < commands.len() {
if pc % 10 == 0 {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]);}
"out" => {do_out(&mut state, split[1]); pc = pc + 1;}
_ => {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