From 52a6d3a28a9f71714bf64b7675e57eaea48a999e Mon Sep 17 00:00:00 2001 From: timeshifter Date: Sun, 5 Jul 2026 16:55:34 +0200 Subject: [PATCH] add day 23 --- day23/Cargo.lock | 7 +++ day23/Cargo.toml | 6 +++ day23/input.txt | 47 +++++++++++++++++ day23/src/main.rs | 127 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 187 insertions(+) create mode 100644 day23/Cargo.lock create mode 100644 day23/Cargo.toml create mode 100644 day23/input.txt create mode 100644 day23/src/main.rs diff --git a/day23/Cargo.lock b/day23/Cargo.lock new file mode 100644 index 0000000..8dd0414 --- /dev/null +++ b/day23/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "day23" +version = "0.1.0" diff --git a/day23/Cargo.toml b/day23/Cargo.toml new file mode 100644 index 0000000..4f0aacb --- /dev/null +++ b/day23/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day23" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/day23/input.txt b/day23/input.txt new file mode 100644 index 0000000..a2b735a --- /dev/null +++ b/day23/input.txt @@ -0,0 +1,47 @@ +jio a, +18 +inc a +tpl a +inc a +tpl a +tpl a +tpl a +inc a +tpl a +inc a +tpl a +inc a +inc a +tpl a +tpl a +tpl a +inc a +jmp +22 +tpl a +inc a +tpl a +inc a +inc a +tpl a +inc a +tpl a +inc a +inc a +tpl a +tpl a +inc a +inc a +tpl a +inc a +inc a +tpl a +inc a +inc a +tpl a +jio a, +8 +inc b +jie a, +4 +tpl a +inc a +jmp +2 +hlf a +jmp -7 diff --git a/day23/src/main.rs b/day23/src/main.rs new file mode 100644 index 0000000..69b88ed --- /dev/null +++ b/day23/src/main.rs @@ -0,0 +1,127 @@ +fn main() { + let instructions: Vec<_> = std::fs::read_to_string("input.txt") + .unwrap() + .lines() + .map(Instruction::parse) + .collect(); + + // part 1: 307 + let mut registers = Register::new(); + + let result = run_program(&instructions, &mut registers); + println!("{result}"); + + // part 2 + let mut registers = Register::new(); + registers.a = 1; + + let result = run_program(&instructions, &mut registers); + println!("{result}"); +} + +fn run_program(instructions: &[Instruction], registers: &mut Register) -> u32 { + let mut pointer = 0; + + while let Some(instruction) = instructions.get(pointer) { + match instruction { + Instruction::Hlf(c) => { + *registers.get_mut(*c) /= 2; + pointer += 1; + } + Instruction::Tpl(c) => { + *registers.get_mut(*c) *= 3; + pointer += 1; + } + Instruction::Inc(c) => { + *registers.get_mut(*c) += 1; + pointer += 1; + } + Instruction::Jmp(offset) => { + pointer = ((pointer as isize) + *offset) as usize; + } + Instruction::Jie(c, offset) => { + if registers.get(*c).is_multiple_of(2) { + pointer = ((pointer as isize) + *offset) as usize; + } else { + pointer += 1; + } + } + Instruction::Jio(c, offset) => { + if registers.get(*c) == 1 { + pointer = ((pointer as isize) + *offset) as usize; + } else { + pointer += 1; + } + } + } + } + + registers.b +} + +#[derive(Debug)] +struct Register { + a: u32, + b: u32, +} + +impl Register { + fn new() -> Self { + Self { a: 0, b: 0 } + } + + fn get(&self, c: char) -> u32 { + match c { + 'a' => self.a, + 'b' => self.b, + _ => panic!("unknown register: {c}"), + } + } + + fn get_mut(&mut self, c: char) -> &mut u32 { + match c { + 'a' => &mut self.a, + 'b' => &mut self.b, + _ => panic!("unknown register: {c}"), + } + } +} + +#[derive(Debug)] +enum Instruction { + Hlf(char), + Tpl(char), + Inc(char), + Jmp(isize), + Jie(char, isize), + Jio(char, isize), +} + +impl Instruction { + fn parse(s: &str) -> Self { + let words: Vec<_> = s.split_whitespace().collect(); + match words[0] { + "hlf" => Self::Hlf(words[1].parse().unwrap()), + "tpl" => Self::Tpl(words[1].parse().unwrap()), + "inc" => Self::Inc(words[1].parse().unwrap()), + "jmp" => Self::Jmp(words[1].parse().unwrap()), + "jie" => Self::Jie( + words[1] + .chars() + .take_while(|c| c.is_alphabetic()) + .next() + .unwrap(), + words[2].parse().unwrap(), + ), + "jio" => Self::Jio( + words[1] + .chars() + .take_while(|c| c.is_alphabetic()) + .next() + .unwrap(), + words[2].parse().unwrap(), + ), + _ => panic!(), + } + } +}