Files
advent-of-code-2015/src/day23.rs
2024-11-01 12:56:24 +01:00

85 lines
2.1 KiB
Rust

use aoc_runner_derive::{aoc, aoc_generator};
type Input = String;
#[aoc_generator(day23)]
fn parse(input: &str) -> Input {
input.to_string()
}
#[aoc(day23, part1)]
fn part1(input: &Input) -> usize {
let mut a = 0;
let mut b = 0;
let mut ip: i32 = 0;
let mem = input.lines().collect::<Vec<_>>();
while let Some(line) = mem.get(ip as usize) {
let (lhs, rhs) = line.split_once(" ").unwrap();
match lhs {
"hlf" => {
match rhs {
"a" => a /= 2,
"b" => b /= 2,
_ => panic!("unknown register"),
};
}
"tpl" => {
match rhs {
"a" => a *= 3,
"b" => b *= 3,
_ => panic!("unknown register"),
};
}
"inc" => {
match rhs {
"a" => a += 1,
"b" => b += 1,
_ => panic!("unknown register"),
};
}
"jmp" => {
let offset = parse_offset(rhs);
ip += offset - 1;
}
"jie" => {
let (r, o) = rhs.split_once(", ").unwrap();
let offset = parse_offset(o);
let jump = match r {
"a" => a % 2 == 0,
"b" => b % 2 == 0,
_ => panic!(),
};
if jump {
ip += offset - 1;
}
}
"jio" => {
let (r, o) = rhs.split_once(", ").unwrap();
let offset = parse_offset(o);
let jump = match r {
"a" => a == 1,
"b" => b == 1,
_ => panic!(),
};
if jump {
ip += offset - 1;
}
}
_ => panic!("unknown instruction"),
}
ip += 1;
}
b
}
fn parse_offset(input: &str) -> i32 {
input.parse().unwrap()
}
#[aoc(day23, part2)]
fn part2(_: &Input) -> usize {
0
}