Advent of code 2022/13
Ajax Direct

Answer

Part 1 :
Part 2 :
function compare($l, $r)
{
    if (is_int($l) && is_int($r)) {
        return $r <=> $l;
    }

    [$l, $r] = [(array) $l, (array) $r];

    while (!empty($l) && !empty($r)) {
        $compare = compare(array_shift($l), array_shift($r));
        if ($compare) return $compare;
    }

    if (empty($l) && empty($r)) return 0;
    if (empty($l)) return 1;
    if (empty($r)) return -1;

    return 0;
}

$pairs = $input->split("\n\n")->map(fn($pair) => $pair->split("\n")->map("json_decode"));

// ==================================================
// > SOLUTIONS
// ==================================================
$solution_1 = $pairs->map((
    fn ($pair, $index) => compare($pair[0], $pair[1]) == 1 ? $index + 1 : 0
), true)->sum();

$solution_2 = $pairs
    ->merge([[[2]], [[6]]])->merge()
    ->sort("compare")->reverse()
    ->map("json_encode")->keep(["[2]", "[6]"])->keys()
    ->map(fn($key) => $key + 1)->multiply();