Getting closer to y2015d22p2

This commit is contained in:
Fabian Schmidt 2024-12-01 19:45:27 +01:00
parent 00692c18df
commit cd1249a5fe

View File

@ -36,6 +36,36 @@ pub fn process_part1(input: &str) -> u32 {
if leaf.state == State::Playing { if leaf.state == State::Playing {
//println!("try for leaf"); //println!("try for leaf");
for spell in Spell::get_all() { for spell in Spell::get_all() {
match spell.name {
SpellID::MagicMissile | SpellID::Drain => {}
SpellID::Shield => {
if !leaf
.player
.status_effects
.iter()
.any(|effect| effect.name == SpellID::Shield && effect.duration > 1)
{
continue;
}
}
SpellID::Poison => {
if !leaf
.player
.status_effects
.iter()
.any(|effect| effect.name == SpellID::Poison && effect.duration > 1)
{
continue;
}
}
SpellID::Recharge => {
if !leaf.player.status_effects.iter().any(|effect| {
effect.name == SpellID::Recharge && effect.duration > 1
}) {
continue;
}
}
}
leafs.push(leaf.use_spell(spell)); leafs.push(leaf.use_spell(spell));
} }
} }
@ -104,26 +134,8 @@ impl RoundNode {
let mut player = self.player.clone(); let mut player = self.player.clone();
let mut boss = self.boss.clone(); let mut boss = self.boss.clone();
// Player turn // Player turn
for effect in player.status_effects.iter_mut() { player.do_status_effects();
if effect.name == SpellID::Recharge { boss.do_status_effects();
player.mana += effect.mana;
}
effect.duration -= 1;
if effect.duration > 0 && effect.name == SpellID::Shield {
player.armor = effect.armor;
} else if effect.name == SpellID::Shield {
player.armor = 0;
}
}
for effect in boss.status_effects.iter_mut() {
boss.hp = boss.hp.saturating_sub(effect.damage);
effect.duration -= 1;
}
boss.status_effects = boss
.status_effects
.into_iter()
.filter(|&effect| effect.duration > 0)
.collect::<Vec<Spell>>();
if boss.hp == 0 { if boss.hp == 0 {
return RoundNode { return RoundNode {
player, player,
@ -132,11 +144,6 @@ impl RoundNode {
state: State::Win, state: State::Win,
}; };
} }
player.status_effects = player
.status_effects
.into_iter()
.filter(|&effect| effect.duration > 0)
.collect::<Vec<Spell>>();
let used_mana = spell.cost; let used_mana = spell.cost;
let mut dmg = 0; let mut dmg = 0;
match spell.name { match spell.name {
@ -182,26 +189,8 @@ impl RoundNode {
} }
// Boss turn // Boss turn
for effect in player.status_effects.iter_mut() { player.do_status_effects();
if effect.name == SpellID::Recharge { boss.do_status_effects();
player.mana += effect.mana;
}
effect.duration -= 1;
if effect.duration > 0 && effect.name == SpellID::Shield {
player.armor = effect.armor;
} else if effect.name == SpellID::Shield {
player.armor = 0;
}
}
player.status_effects = player
.status_effects
.into_iter()
.filter(|&effect| effect.duration > 0)
.collect::<Vec<Spell>>();
for effect in boss.status_effects.iter_mut() {
boss.hp = boss.hp.saturating_sub(effect.damage);
effect.duration -= 1;
}
if boss.hp == 0 { if boss.hp == 0 {
return RoundNode { return RoundNode {
player, player,
@ -210,11 +199,6 @@ impl RoundNode {
state: State::Win, state: State::Win,
}; };
} }
boss.status_effects = boss
.status_effects
.into_iter()
.filter(|&effect| effect.duration > 0)
.collect::<Vec<Spell>>();
let dmg = boss.hit_damage(&player); let dmg = boss.hit_damage(&player);
player.hp = player.hp.saturating_sub(dmg); player.hp = player.hp.saturating_sub(dmg);
@ -344,6 +328,29 @@ impl Character {
1 1
} }
} }
fn do_status_effects(&mut self) {
self.status_effects.iter_mut().for_each(|effect| {
if effect.name == SpellID::Recharge {
self.mana += effect.mana;
}
if effect.name == SpellID::Poison {
self.hp -= effect.damage;
}
effect.duration -= 1;
if effect.duration > 0 && effect.name == SpellID::Shield {
self.armor = effect.armor;
} else if effect.name == SpellID::Shield {
self.armor = 0;
}
});
self.status_effects = self
.status_effects
.clone()
.into_iter()
.filter(|&effect| effect.duration > 0)
.collect::<Vec<Spell>>();
}
} }
#[cfg(test)] #[cfg(test)]