input as $line){ [$left[], $right[]] = array_map("intval", explode(" ", $line)); } sort($left); sort($right); $sum = 0; for ($i = 0; $i < count($left); $i++){ $sum += abs($left[$i] - $right[$i]); } return strval($sum); } function part2(): string { $left = []; $rightCounts = []; foreach ($this->input as $line){ [$left[], $right] = array_map("intval", explode(" ", $line)); if (array_key_exists($right, $rightCounts)){ $rightCounts[$right]++; } else { $rightCounts[$right] = 1; } } $sum = 0; foreach ($left as $number){ $sum += $number * ($rightCounts[$number] ?? 0); } return strval($sum); } }