add day 23
This commit is contained in:
Generated
+7
@@ -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"
|
||||
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "day23"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
@@ -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
|
||||
@@ -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!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user