y2015d22 new approach, no recursion, still broken

This commit is contained in:
Fabian Schmidt 2024-11-15 16:01:09 +01:00
parent 46deb678df
commit 00692c18df

View File

@ -1,3 +1,5 @@
use core::panic;
pub fn process_part1(input: &str) -> u32 { pub fn process_part1(input: &str) -> u32 {
let mut boss = Character::default(); let mut boss = Character::default();
input.lines().for_each(|line| { input.lines().for_each(|line| {
@ -13,16 +15,63 @@ pub fn process_part1(input: &str) -> u32 {
mana: 500, mana: 500,
..Default::default() ..Default::default()
}; };
let mut used_mana = Vec::new(); // Initial state, nothing happened yet
for spell in Spell::get_all() { let start = RoundNode {
used_mana.push(round(player.clone(), boss.clone(), spell, 0)); player,
} boss,
spent_mana: 0,
state: State::Playing,
};
let mut smallest = u32::MAX; let mut smallest = u32::MAX;
for state in used_mana { let mut leafs = vec![start];
if state.get_value() < smallest { let mut test = 0;
smallest = state.get_value(); println!("start");
loop {
println!("iter: {test},");
let current_leafs = leafs.clone();
println!("leaf count: {}", current_leafs.len());
leafs.clear();
// create potential rounds
for leaf in current_leafs {
if leaf.state == State::Playing {
//println!("try for leaf");
for spell in Spell::get_all() {
leafs.push(leaf.use_spell(spell));
} }
} }
}
for leaf in &leafs {
if leaf.state == State::Win && leaf.spent_mana < smallest {
smallest = leaf.spent_mana;
}
//println!(
// "player hp: {}, boss hp: {}, spent mana so far: {}",
// leaf.player.hp, leaf.boss.hp, leaf.spent_mana
//);
}
leafs = leafs
.clone()
.iter()
.filter(|&leaf| leaf.state == State::Playing || leaf.spent_mana <= smallest)
.map(|leaf| leaf.to_owned())
.collect();
if leafs.is_empty() {
break;
}
// already know it's higher than this
if smallest <= 2000 {
let mut count_losses = 0;
for leaf in leafs {
if leaf.state == State::Loss {
count_losses += 1;
}
}
println!("Losses: {count_losses}");
break;
}
test += 1;
println!(" smallest: {smallest}");
}
smallest smallest
} }
@ -30,7 +79,30 @@ pub fn process_part2(input: &str) -> u32 {
0 0
} }
fn round(mut player: Character, mut boss: Character, spell: Spell, spent_so_far: u32) -> State { #[derive(Debug, Clone, PartialEq, Eq)]
enum State {
Playing,
Win,
Loss,
}
#[derive(Debug, Clone)]
struct RoundNode {
player: Character,
boss: Character,
spent_mana: u32,
state: State,
}
impl RoundNode {
fn use_spell(&self, spell: Spell) -> RoundNode {
if self.state != State::Playing {
eprintln!("State: {:#?}", self.state);
eprintln!("spell: {spell:#?}");
panic!("State should be Playing");
}
let mut player = self.player.clone();
let mut boss = self.boss.clone();
// Player turn // Player turn
for effect in player.status_effects.iter_mut() { for effect in player.status_effects.iter_mut() {
if effect.name == SpellID::Recharge { if effect.name == SpellID::Recharge {
@ -52,12 +124,20 @@ fn round(mut player: Character, mut boss: Character, spell: Spell, spent_so_far:
.into_iter() .into_iter()
.filter(|&effect| effect.duration > 0) .filter(|&effect| effect.duration > 0)
.collect::<Vec<Spell>>(); .collect::<Vec<Spell>>();
if boss.hp == 0 {
return RoundNode {
player,
boss,
spent_mana: self.spent_mana,
state: State::Win,
};
}
player.status_effects = player player.status_effects = player
.status_effects .status_effects
.into_iter() .into_iter()
.filter(|&effect| effect.duration > 0) .filter(|&effect| effect.duration > 0)
.collect::<Vec<Spell>>(); .collect::<Vec<Spell>>();
let used_mana = spell.cost + spent_so_far; let used_mana = spell.cost;
let mut dmg = 0; let mut dmg = 0;
match spell.name { match spell.name {
SpellID::MagicMissile => { SpellID::MagicMissile => {
@ -85,10 +165,20 @@ fn round(mut player: Character, mut boss: Character, spell: Spell, spent_so_far:
} }
boss.hp = boss.hp.saturating_sub(dmg); boss.hp = boss.hp.saturating_sub(dmg);
if boss.hp == 0 { if boss.hp == 0 {
return State::Win(used_mana); return RoundNode {
player,
boss,
spent_mana: used_mana + self.spent_mana,
state: State::Win,
};
} }
if player.mana == 0 { if player.mana == 0 {
return State::Loss(0); return RoundNode {
player,
boss,
spent_mana: used_mana + self.spent_mana,
state: State::Loss,
};
} }
// Boss turn // Boss turn
@ -113,7 +203,12 @@ fn round(mut player: Character, mut boss: Character, spell: Spell, spent_so_far:
effect.duration -= 1; effect.duration -= 1;
} }
if boss.hp == 0 { if boss.hp == 0 {
return State::Win(used_mana); return RoundNode {
player,
boss,
spent_mana: used_mana + self.spent_mana,
state: State::Win,
};
} }
boss.status_effects = boss boss.status_effects = boss
.status_effects .status_effects
@ -124,45 +219,39 @@ fn round(mut player: Character, mut boss: Character, spell: Spell, spent_so_far:
player.hp = player.hp.saturating_sub(dmg); player.hp = player.hp.saturating_sub(dmg);
if player.hp == 0 { if player.hp == 0 {
return State::Loss(0); return RoundNode {
} player,
boss,
// next round spent_mana: used_mana + self.spent_mana,
let mut smallest_used_mana = State::Playing(u32::MAX); state: State::Loss,
for spell in Spell::get_all() {
let next_state = round(player.clone(), boss.clone(), spell, used_mana);
match next_state {
State::Playing(mana) => {
if mana < smallest_used_mana.get_value() {
smallest_used_mana = State::Playing(mana);
}
}
State::Win(mana) => {
if mana < smallest_used_mana.get_value() {
smallest_used_mana = State::Win(mana);
}
}
State::Loss(_) => {}
}; };
} }
smallest_used_mana RoundNode {
player,
boss,
spent_mana: used_mana + self.spent_mana,
state: State::Playing,
}
} }
#[derive(Debug)] //fn get_leafs(&self) -> Vec<RoundNode> {
enum State { // let mut leafs = Vec::new();
Playing(u32), // match self.state {
Win(u32), // State::Playing => {
Loss(u32), // if self.next_rounds.is_empty() {
} // return vec![self.clone()];
// }
impl State { // let mut round_leafs = Vec::new();
fn get_value(&self) -> u32 { // for round in self.next_rounds.clone() {
match self { // round_leafs = round.get_leafs();
State::Playing(value) => *value, // }
State::Win(value) => *value, // leafs.append(&mut round_leafs);
State::Loss(value) => *value, // leafs
} // }
} // State::Win => vec![self.clone()],
// State::Loss => vec![self.clone()],
// }
//}
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
@ -273,17 +362,49 @@ mod tests {
mana: 250, mana: 250,
..Default::default() ..Default::default()
}; };
let mut used_mana = Vec::new(); // Initial state, nothing happened yet
let start = RoundNode {
player,
boss,
spent_mana: 0,
state: State::Playing,
};
let mut smallest = u32::MAX;
let mut leafs = vec![start];
let mut test = 0;
println!("start");
loop {
println!("iter: {test},");
let current_leafs = leafs.clone();
leafs.clear();
// create potential rounds
for leaf in current_leafs {
if leaf.state == State::Playing {
for spell in Spell::get_all() { for spell in Spell::get_all() {
used_mana.push(round(player.clone(), boss.clone(), spell, 0)); leafs.push(leaf.use_spell(spell));
}
let mut result = u32::MAX;
for state in used_mana {
if state.get_value() < result {
result = state.get_value();
} }
} }
assert_eq!(result, 226); }
let mut all_done = true;
for leaf in &leafs {
if leaf.state == State::Win && leaf.spent_mana < smallest {
smallest = leaf.spent_mana;
}
if leaf.state == State::Playing {
all_done = false;
}
println!(
"player hp: {}, boss hp: {}, spent mana so far: {}",
leaf.player.hp, leaf.boss.hp, leaf.spent_mana
);
}
if all_done {
break;
}
test += 1;
println!(" smallest: {smallest}");
}
assert_eq!(smallest, 226);
} }
#[test] #[test]
@ -298,17 +419,49 @@ mod tests {
mana: 250, mana: 250,
..Default::default() ..Default::default()
}; };
let mut used_mana = Vec::new(); // Initial state, nothing happened yet
let start = RoundNode {
player,
boss,
spent_mana: 0,
state: State::Playing,
};
let mut smallest = u32::MAX;
let mut leafs = vec![start];
let mut test = 0;
println!("start");
loop {
println!("iter: {test},");
let current_leafs = leafs.clone();
leafs.clear();
// create potential rounds
for leaf in current_leafs {
if leaf.state == State::Playing {
for spell in Spell::get_all() { for spell in Spell::get_all() {
used_mana.push(round(player.clone(), boss.clone(), spell, 0)); leafs.push(leaf.use_spell(spell));
}
let mut result = u32::MAX;
for state in used_mana {
if state.get_value() < result {
result = state.get_value();
} }
} }
assert_eq!(result, 641); }
let mut all_done = true;
for leaf in &leafs {
if leaf.state == State::Win && leaf.spent_mana < smallest {
smallest = leaf.spent_mana;
}
if leaf.state == State::Playing {
all_done = false;
}
println!(
"player hp: {}, boss hp: {}, spent mana so far: {}",
leaf.player.hp, leaf.boss.hp, leaf.spent_mana
);
}
if all_done {
break;
}
test += 1;
println!(" smallest: {smallest}");
}
assert_eq!(smallest, 641);
} }
#[test] #[test]