diff --git a/y2015/src/bin/d23.rs b/y2015/src/bin/d23.rs index 81cb89e..3e66898 100644 --- a/y2015/src/bin/d23.rs +++ b/y2015/src/bin/d23.rs @@ -10,11 +10,11 @@ fn main() { fn part1() { let root = env!("CARGO_MANIFEST_DIR"); let content = fs::read_to_string(format!("{root}/resources/23_input.txt")).unwrap(); - println!("{}", d23::process_part1(&content)); + println!("{:?}", d23::process_part1(&content)); } fn part2() { let root = env!("CARGO_MANIFEST_DIR"); let content = fs::read_to_string(format!("{root}/resources/23_input.txt")).unwrap(); - println!("{}", d23::process_part2(&content)); + println!("{:?}", d23::process_part2(&content)); } diff --git a/y2015/src/days/d23.rs b/y2015/src/days/d23.rs index 7707bd0..06d02b2 100644 --- a/y2015/src/days/d23.rs +++ b/y2015/src/days/d23.rs @@ -1,25 +1,135 @@ -enum Instruction { - Half, - Triple, - Increment, - Jump, - JumpEven, - JumpOne, -} +use core::panic; -pub fn process_part1(input: &str) -> i32 { +pub fn process_part1(input: &str) -> (i32, i32) { let mut a = 0; let mut b = 0; - let lines = input + let instructions = input .lines() .map(|line| line.to_string()) .collect::>(); - let mut idx = 0; - b + let mut idx: i32 = 0; + loop { + if let Some(instruction) = instructions.get(idx as usize) { + let (instruction_name, instruction_action) = instruction.split_once(" ").unwrap(); + match instruction_name { + "hlf" => { + if instruction_action == "a" { + a /= 2; + } else if instruction_action == "b" { + b /= 2; + } + idx += 1; + } + "tpl" => { + if instruction_action == "a" { + a *= 3; + } else if instruction_action == "b" { + b *= 3; + } + idx += 1; + } + "inc" => { + if instruction_action == "a" { + a += 1; + } else if instruction_action == "b" { + b += 1; + } + idx += 1; + } + "jmp" => { + let offset = instruction_action.parse::().unwrap(); + idx += offset; + } + "jie" => { + let (reg, offset) = instruction_action.split_once(", ").unwrap(); + let offset: i32 = offset.parse().unwrap(); + if (reg == "a" && a % 2 == 0) || (reg == "b" && b % 2 == 0) { + idx += offset; + } else { + idx += 1; + } + } + "jio" => { + let (reg, offset) = instruction_action.split_once(", ").unwrap(); + let offset: i32 = offset.parse().unwrap(); + if (reg == "a" && a == 1) || (reg == "b" && b == 1) { + idx += offset; + } else { + idx += 1; + } + } + _ => panic!("Unknown instruction {instruction_name}"), + } + } else { + return (a, b); + } + } } -pub fn process_part2(input: &str) -> i32 { - 0 +pub fn process_part2(input: &str) -> (i32, i32) { + let mut a = 1; + let mut b = 0; + let instructions = input + .lines() + .map(|line| line.to_string()) + .collect::>(); + let mut idx: i32 = 0; + loop { + if let Some(instruction) = instructions.get(idx as usize) { + let (instruction_name, instruction_action) = instruction.split_once(" ").unwrap(); + match instruction_name { + "hlf" => { + if instruction_action == "a" { + a /= 2; + } else if instruction_action == "b" { + b /= 2; + } + idx += 1; + } + "tpl" => { + if instruction_action == "a" { + a *= 3; + } else if instruction_action == "b" { + b *= 3; + } + idx += 1; + } + "inc" => { + if instruction_action == "a" { + a += 1; + } else if instruction_action == "b" { + b += 1; + } + idx += 1; + } + "jmp" => { + let offset = instruction_action.parse::().unwrap(); + idx += offset; + } + "jie" => { + let (reg, offset) = instruction_action.split_once(", ").unwrap(); + let offset: i32 = offset.parse().unwrap(); + if (reg == "a" && a % 2 == 0) || (reg == "b" && b % 2 == 0) { + idx += offset; + } else { + idx += 1; + } + } + "jio" => { + let (reg, offset) = instruction_action.split_once(", ").unwrap(); + let offset: i32 = offset.parse().unwrap(); + if (reg == "a" && a == 1) || (reg == "b" && b == 1) { + idx += offset; + } else { + idx += 1; + } + } + _ => panic!("Unknown instruction {instruction_name}"), + } + } else { + return (a, b); + } + } } #[cfg(test)] @@ -34,12 +144,12 @@ inc a"; #[test] fn part1() { let result = process_part1(INPUT); - assert_eq!(result, 2); + assert_eq!(result.0, 2); } #[test] fn part2() { let result = process_part2(INPUT); - assert_eq!(result, 0); + assert_eq!(result.0, 7); } }