From 241c417a77e0728128cf7f682b5bf70a5e5de677 Mon Sep 17 00:00:00 2001 From: Johannes Date: Tue, 14 Nov 2023 16:56:16 +0100 Subject: [PATCH] intermediate --- src/main.rs | 2 +- src/tasks/day16.rs | 2 +- src/tasks/day24.rs | 69 +++++++++++++++++++++++++++------------------- 3 files changed, 43 insertions(+), 30 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6c2914f..e0d66ee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,3 @@ fn main() { - aoc_2018::tasks::day21::task1(); + aoc_2018::tasks::day24::task1(); } diff --git a/src/tasks/day16.rs b/src/tasks/day16.rs index 8abbd04..6e182de 100644 --- a/src/tasks/day16.rs +++ b/src/tasks/day16.rs @@ -266,7 +266,7 @@ struct Instruction { #[cfg(test)] mod test { - use crate::tasks::day16::{Instruction, matches_count, Op, parse, Registers}; + use crate::tasks::day16::{matches_count, parse, Instruction, Op, Registers}; #[test] fn example1() { diff --git a/src/tasks/day24.rs b/src/tasks/day24.rs index 92054cb..d5b7e4e 100644 --- a/src/tasks/day24.rs +++ b/src/tasks/day24.rs @@ -1,16 +1,18 @@ -use crate::utils; use std::cmp::Reverse; use std::collections::HashMap; -pub fn task1() { - let _input = "Immune System: - 17 units each with 5390 hit points (weak to radiation, bludgeoning) with an attack that does 4507 fire damage at initiative 2 - 989 units each with 1274 hit points (immune to fire; weak to bludgeoning, slashing) with an attack that does 25 slashing damage at initiative 3 +use crate::utils; -Infection: - 801 units each with 4706 hit points (weak to radiation) with an attack that does 116 bludgeoning damage at initiative 1 - 4485 units each with 2961 hit points (immune to radiation; weak to fire, cold) with an attack that does 12 slashing damage at initiative 4"; +pub fn task1() { let input = utils::read_file("input/day24.txt"); + + let remaining_units = play(&input); + println!("Standing units after the game: {}", remaining_units); + // 21107 too high + // 21004 too high +} + +fn play(input: &str) -> i64 { let mut game = Game::create(&input); println!( @@ -39,13 +41,7 @@ Infection: println!("{:#?}", game); println!("Played {} rounds", rounds_played); - println!( - "Standing units after the game: {}", - game.groups.values().map(|it| it.units).sum::() - ); - - // 21107 too high - // 21004 too high + game.groups.values().map(|it| it.units).sum::() } #[derive(Debug)] @@ -81,15 +77,15 @@ impl Game { // lock targets ordered by effective power let mut all_by_power: Vec<&Group> = self.groups.values().collect(); all_by_power.sort_unstable_by_key(|a| Reverse((a.effective_power(), a.initiative))); - // for group in all_by_power.iter() { - // println!( - // "{}: {} ({})", - // group.id, - // group.effective_power(), - // group.initiative - // ); - // } - // println!("{:?}", all_by_power); + for group in all_by_power.iter() { + println!( + "{}: {} ({})", + group.id, + group.effective_power(), + group.initiative + ); + } + println!("{:?}", all_by_power); for group in all_by_power.iter() { if let Some(t) = self .groups @@ -142,10 +138,10 @@ impl Game { if let Some(enemy) = self.groups.get_mut(enemy_id) { enemy.units -= dying_units; } - // println!( - // "{} dealt {} ({} units) damage to {}", - // active_id, damage, dying_units, enemy_id - // ); + println!( + "{} dealt {} ({} units) damage to {}", + active_id, damage, dying_units, enemy_id + ); } } @@ -246,3 +242,20 @@ impl Group { } } } + +#[cfg(test)] +mod test { + use crate::tasks::day24::play; + + #[test] + fn example1() { + let input = "Immune System: + 17 units each with 5390 hit points (weak to radiation, bludgeoning) with an attack that does 4507 fire damage at initiative 2 + 989 units each with 1274 hit points (immune to fire; weak to bludgeoning, slashing) with an attack that does 25 slashing damage at initiative 3 + +Infection: + 801 units each with 4706 hit points (weak to radiation) with an attack that does 116 bludgeoning damage at initiative 1 + 4485 units each with 2961 hit points (immune to radiation; weak to fire, cold) with an attack that does 12 slashing damage at initiative 4"; + assert_eq!(5216, play(input)); + } +}