y2015d23 refreshingly easy

This commit is contained in:
Fabian Schmidt 2024-12-03 13:33:49 +01:00
parent daead25413
commit 0b4e2449a1
2 changed files with 128 additions and 18 deletions

View File

@ -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));
}

View File

@ -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::<Vec<String>>();
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::<i32>().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::<Vec<String>>();
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::<i32>().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);
}
}