From 4159e3af14928e290aa2291f22d3a75b93200a55 Mon Sep 17 00:00:00 2001 From: Johannes Date: Fri, 1 Nov 2024 12:56:24 +0100 Subject: [PATCH] day 23 part 1 --- src/day22.rs | 9 +++--- src/day23.rs | 80 +++++++++++++++++++++++++++++++++++++++++++++++----- src/lib.rs | 1 + 3 files changed, 78 insertions(+), 12 deletions(-) diff --git a/src/day22.rs b/src/day22.rs index 1a786af..8bbb87a 100644 --- a/src/day22.rs +++ b/src/day22.rs @@ -228,6 +228,9 @@ fn play(boss: Boss, hard: bool) -> i32 { let mut min_mana = i32::MAX; while let Some(current) = queue.pop() { + if current.boss.health < 1 { + return current.wizard.spent; + } if INTERACTIVE { let _ = std::io::stdin().read_line(&mut String::new()).unwrap(); println!("{current:?}"); @@ -289,14 +292,10 @@ fn play(boss: Boss, hard: bool) -> i32 { let armor = n.wizard.armor(); let mut n = n.apply_effects(); if n.boss.health < 1 { - min_mana = min(min_mana, n.wizard.spent); - queue.retain(|s| s.wizard.spent > min_mana); } else { n.wizard.health -= max(1, n.boss.damage - armor); - if n.wizard.health > 0 && n.wizard.spent < min_mana { - queue.push(n); - } } + queue.push(n); } } return min_mana; diff --git a/src/day23.rs b/src/day23.rs index 11fa780..b8a8258 100644 --- a/src/day23.rs +++ b/src/day23.rs @@ -1,18 +1,84 @@ use aoc_runner_derive::{aoc, aoc_generator}; -type Input = (); +type Input = String; -#[aoc_generator(day22)] +#[aoc_generator(day23)] fn parse(input: &str) -> Input { - todo!() + input.to_string() } -#[aoc(day22, part1)] -fn part1(_: &Input) -> usize { - 0 +#[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::>(); + + 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 } -#[aoc(day22, part2)] +fn parse_offset(input: &str) -> i32 { + input.parse().unwrap() +} + +#[aoc(day23, part2)] fn part2(_: &Input) -> usize { 0 } diff --git a/src/lib.rs b/src/lib.rs index 11ebd92..41efe44 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,5 +15,6 @@ mod day19; mod day20; mod day21; mod day22; +mod day23; aoc_lib! { year = 2015 }