$repl = (array) set(range("a", "z"))->map(fn ($l) =>
    [$l . strtoupper($l), strtoupper($l) . $l]
)->merge();
// React item until no more reactions are possible
function react($item, $repl) {
    $reaction = str_replace($repl, "", $item);
    if ($reaction === $item) return $reaction;
    return react($reaction, $repl);
}
// ==================================================
// > PART 1
// ==================================================
$solution_1 = strlen(react($input->string, $repl));
// ==================================================
// > PART 2
// ==================================================
$solution_2 = set(range("a", "z"))->map(fn ($l) =>
    strlen(react($input->replace($l, "")->replace(strtoupper($l), "")->string, $repl))
)->min();