intermediate

This commit is contained in:
2023-11-14 16:56:16 +01:00
parent 4cbe831115
commit 241c417a77
3 changed files with 43 additions and 30 deletions

View File

@@ -1,3 +1,3 @@
fn main() {
aoc_2018::tasks::day21::task1();
aoc_2018::tasks::day24::task1();
}

View File

@@ -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() {

View File

@@ -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::<i64>()
);
// 21107 too high
// 21004 too high
}
#[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));
}
}