[$boss_health, $boss_dmg] = $input->numbers();
$spells = [
"Magic Missile" => [53, 4, 0, 0, 0, 0],
"Drain" => [73, 2, 2, 0, 0, 0],
"Shield" => [113, 0, 0, 6, 7, 0],
"Poison" => [173, 3, 0, 6, 0, 0],
"Recharge" => [229, 0, 0, 5, 0, 101],
];
$graph = new Graph(function ($graph) use ($boss_dmg, $spells) {
[$turn, $hp, $mana, $boss, $e1, $e2, $e3] = array_map("intval", explode("|", $graph->current));
if ($e2) $boss -= $spells["Poison"][1];
if ($e3) $mana += $spells["Recharge"][5];
$e1 = max(0, $e1 - 1);
$e2 = max(0, $e2 - 1);
$e3 = max(0, $e3 - 1);
if (!($turn % 2)) {
$hp -= max(1, $boss_dmg - ($e1 ? $spells["Shield"][4] : 0));
return [($turn+1)."|$hp|$mana|$boss|$e1|$e2|$e3" => 0];
}
if (!empty($graph->hardMode)) {
$hp--;
if ($hp <= 0) return [];
}
$next_turn = [];
foreach ($spells as $spell => [$cost, $dmg, $heal, $duration]) {
if ($mana < $cost) continue;
if ($e1 && $spell == "Shield") continue;
if ($e2 && $spell == "Poison") continue;
if ($e3 && $spell == "Recharge") continue;
$next_turn[join("|", [
$turn + 1,
$hp + $heal,
$mana - $cost,
$boss - ($duration ? 0 : $dmg),
$spell == "Shield" ? $duration : $e1,
$spell == "Poison" ? $duration : $e2,
$spell == "Recharge" ? $duration : $e3
])] = $cost;
}
return $next_turn;
});
$graph->defineEnd(function ($graph) {
$state = explode("|", $graph->current);
return $state[1] > 0 && $state[2] > 0 && $state[3] <= 0;
});
$solution_1 = $graph->explore("-1|50|500|$boss_health|0|0|0")[1];
$graph->hardMode = true;
$solution_2 = $graph->explore("-1|50|500|$boss_health|0|0|0")[1];