Skip to content

Commit

Permalink
cargo fmt day23
Browse files Browse the repository at this point in the history
  • Loading branch information
LD250 committed Apr 7, 2016
1 parent baa7d5b commit 57f1f9a
Showing 1 changed file with 46 additions and 24 deletions.
70 changes: 46 additions & 24 deletions day23_v2/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//#![feature(slice_patterns)]
// #![feature(slice_patterns)]
use std::io::prelude::*;
use std::fs::File;
use std::collections::HashMap;
Expand All @@ -12,7 +12,7 @@ fn main() {
Ok(mut file) => {
file.read_to_string(&mut content).unwrap();
content = content.trim().replace(",", "");
},
}
};

let lines: Vec<&str> = content.split("\n").collect();
Expand All @@ -22,24 +22,36 @@ fn main() {
instr: &'a str,
value: i32,
};

let mut operations: Vec<Operation> = Vec::new();

for line in &lines {
let ops: Vec<&str> = line.split(' ').collect();
let op = match ops[0] {
r @ "jmp" => Operation {reg: "",
instr: r,
value: ops[1].parse().expect(ops[1])},
r @ "jio" | r @ "jie" => Operation {reg: ops[1],
instr: r,
value: ops[2].parse().expect(ops[2])},
r => Operation {reg: ops[1],
instr: r,
value: 0},
r @ "jmp" => {
Operation {
reg: "",
instr: r,
value: ops[1].parse().expect(ops[1]),
}
}
r @ "jio" | r @ "jie" => {
Operation {
reg: ops[1],
instr: r,
value: ops[2].parse().expect(ops[2]),
}
}
r => {
Operation {
reg: ops[1],
instr: r,
value: 0,
}
}
};
operations.push(op);
};
}

fn run_program(reg_a_value: i32, operations: &Vec<Operation>) {
let mut regs = HashMap::new();
Expand All @@ -50,34 +62,44 @@ fn main() {
while i < operations.len() {
match operations[i] {

Operation {reg, instr, value} => {
Operation { reg, instr, value } => {
let mut plus: i32 = 1;
match instr {
"jmp" => plus = value,
other => {
let reg_value = match regs.get(&reg){
let reg_value = match regs.get(&reg) {
Some(&val) => val,
_ => panic!("register not found")
_ => panic!("register not found"),
};
match other {
"hlf" => {regs.insert(reg, reg_value / 2);},
"tpl" => {regs.insert(reg, reg_value * 3);},
"inc" => {regs.insert(reg, reg_value + 1);},
"jie" => if reg_value % 2 == 0 {plus = value},
"hlf" => {
regs.insert(reg, reg_value / 2);
}
"tpl" => {
regs.insert(reg, reg_value * 3);
}
"inc" => {
regs.insert(reg, reg_value + 1);
}
"jie" => {
if reg_value % 2 == 0 {
plus = value
}
}
"jio" => {
if reg_value == 1 {
plus = value;
};
},
}
_ => panic!("out"),
};
}
};
plus = i as i32 + plus;
i = plus as usize;
},
}
}
};
}
println!("{:?}", regs);
};
run_program(0, &operations);
Expand Down

0 comments on commit 57f1f9a

Please sign in to comment.