$real_rooms = $input->lines->map(function ($room) {
preg_match("/([a-z\-]+)\-([0-9]+)\[([a-z]+)\]/", $room, $matches);
[$_, $letters, $sector, $checksum] = $matches;
$value = set(str_split($letters))->remove("-")
->occurrences()
->sortKeys()->sort(fn ($a, $b) => $b <=> $a)
->keys()->slice(0, 5)->join("");
if ($value == $checksum) return compact("letters", "sector", "checksum");
return false;
})->filter();
// ==================================================
// > PART 1
// ==================================================
$solution_1 = $real_rooms->column("sector")->sum();
// ==================================================
// > PART 2
// ==================================================
foreach ($real_rooms as $room) {
$decode = "";
foreach (str_split($room["letters"]) as $i=>$letter) {
if ($letter != "-") $decode[$i] = chr(((ord($letter) - 96) + $room["sector"]) % 26 + 96);
}
if (strpos($decode , "northpole") !== false) break;
}
$solution_2 = $room["sector"];