diff --git a/y2015/src/days/d22.rs b/y2015/src/days/d22.rs index f0c7a19..54acd95 100644 --- a/y2015/src/days/d22.rs +++ b/y2015/src/days/d22.rs @@ -36,6 +36,36 @@ pub fn process_part1(input: &str) -> u32 { if leaf.state == State::Playing { //println!("try for leaf"); 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)); } } @@ -104,26 +134,8 @@ impl RoundNode { let mut player = self.player.clone(); let mut boss = self.boss.clone(); // Player turn - for effect in player.status_effects.iter_mut() { - if effect.name == SpellID::Recharge { - 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::>(); + player.do_status_effects(); + boss.do_status_effects(); if boss.hp == 0 { return RoundNode { player, @@ -132,11 +144,6 @@ impl RoundNode { state: State::Win, }; } - player.status_effects = player - .status_effects - .into_iter() - .filter(|&effect| effect.duration > 0) - .collect::>(); let used_mana = spell.cost; let mut dmg = 0; match spell.name { @@ -182,26 +189,8 @@ impl RoundNode { } // Boss turn - for effect in player.status_effects.iter_mut() { - if effect.name == SpellID::Recharge { - 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::>(); - for effect in boss.status_effects.iter_mut() { - boss.hp = boss.hp.saturating_sub(effect.damage); - effect.duration -= 1; - } + player.do_status_effects(); + boss.do_status_effects(); if boss.hp == 0 { return RoundNode { player, @@ -210,11 +199,6 @@ impl RoundNode { state: State::Win, }; } - boss.status_effects = boss - .status_effects - .into_iter() - .filter(|&effect| effect.duration > 0) - .collect::>(); let dmg = boss.hit_damage(&player); player.hp = player.hp.saturating_sub(dmg); @@ -344,6 +328,29 @@ impl Character { 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::>(); + } } #[cfg(test)]