// Remove cancelled characters
$input = preg_replace("/\!./", "", $input);
// Remove garbage
$input = preg_replace_callback("/<(.*?)>/", fn ($a) =>
str_repeat("#", strlen($a[1]))
, $input);
// Count groups
$groups = 0;
$score = 0;
for ($i = 0; $i < strlen($input); $i++) {
if ($input[$i] == "{") {
$groups++;
$score += $groups;
} elseif ($input[$i] == "}") {
$groups--;
}
}
// ==================================================
// > SOLUTIONS
// ==================================================
$solution_1 = $score;
$solution_2 = substr_count($input, "#");