$nodes = $input->lines->mapAssoc(fn ($i, $line) =>
[$line->numbers()[0] => array_fill_keys((array) $line->numbers()->slice(1), 1)]
);
function get_group($nodes, $start) {
$queue = [$start];
$visited = [];
while (!empty($queue)) {
$current = array_shift($queue);
$visited[] = $current;
foreach ($nodes[$current] as $state => $relation) {
if (!in_array($state, $queue) && !in_array($state, $visited)) {
$queue[] = $state;
}
}
}
return $visited;
}
// ==================================================
// > PART 1
// ==================================================
$solution_1 = count(get_group($nodes, 0));
// ==================================================
// > PART 2
// ==================================================
$solution_2 = 0;
while (!$nodes->empty()) {
$solution_2++;
$group = get_group($nodes, $nodes->keys()[0]);
$nodes = $nodes->removeKeys($group);
}