use core::read_data; #[derive(Default)] struct Computer { pub a: u32, pub b: u32, pub pc: i32, } impl Computer { fn dump(&self) { println!("DUMPING COMPUTER"); println!("A: {}\tB: {}\tPC: {}", self.a, self.b, self.pc ); } } fn main() { let mut computer = Computer::default(); let binding = read_data("2015_23_data.txt"); let lines = binding.lines(); for (index, line) in lines.enumerate() { println!("Line {index} = [[{line}]]"); let (instruction, params) = line.split_once(" ").unwrap(); match instruction { "inc" => { match params.trim().chars().next().unwrap() { 'a' => { computer.a += 1; }, 'b' => { computer.b += 1; }, _ => { unreachable!("Invalid Register"); } } }, "tpl" => { match params.trim().chars().next().unwrap() { 'a' => { computer.a = computer.a * 3; }, 'b' => { computer.b = computer.b * 3; }, _ => { unreachable!("Invalid Register") } } }, "hlf" => { println!("HLF"); match params.trim().chars().next().unwrap() { 'a' => { computer.a = computer.a / 2; } 'b' => { computer.b = computer.b / 2; } _ => { unreachable!("Invalid Register"); } } }, "jmp" => { println!("JMP Parse from [[{params}]]"); let offset = params.trim().parse::().unwrap(); computer.pc += offset; }, "jie" => { let (register, offset) = params.trim().split_once(' ').unwrap(); let offset = offset.parse::().unwrap(); match register.trim().chars().next().unwrap() { 'a' => { if computer.a.is_multiple_of(2) { computer.pc += offset; } }, 'b' => { if computer.b.is_multiple_of(2) { computer.pc += offset; } }, _ => { unreachable!("Invalid Register"); } } }, "jio" => { let (register, offset) = params.trim().split_once(' ').unwrap(); let offset = offset.parse::().unwrap(); match register.trim().chars().next().unwrap() { 'a' => { if !computer.a.is_multiple_of(2) { computer.pc += offset; } }, 'b' => { if !computer.b.is_multiple_of(2) { computer.pc += offset; } }, _ => { unreachable!("Invalid Register"); } } }, _ => { println!("Unhandled -> {instruction}"); } } computer.dump(); } println!("Program loaded..."); computer.dump(); }