From 57f1f9ad9f098f35cbd057344317a45409de55a7 Mon Sep 17 00:00:00 2001 From: Denys Levchenko Date: Thu, 7 Apr 2016 15:20:12 +0300 Subject: [PATCH] cargo fmt day23 --- day23_v2/src/main.rs | 70 +++++++++++++++++++++++++++++--------------- 1 file changed, 46 insertions(+), 24 deletions(-) diff --git a/day23_v2/src/main.rs b/day23_v2/src/main.rs index 8127dd1..1bd0243 100644 --- a/day23_v2/src/main.rs +++ b/day23_v2/src/main.rs @@ -1,4 +1,4 @@ -//#![feature(slice_patterns)] +// #![feature(slice_patterns)] use std::io::prelude::*; use std::fs::File; use std::collections::HashMap; @@ -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(); @@ -22,24 +22,36 @@ fn main() { instr: &'a str, value: i32, }; - + let mut operations: Vec = 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) { let mut regs = HashMap::new(); @@ -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(®){ + let reg_value = match regs.get(®) { 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);