Advent of code 2021/8
Ajax Direct

Answer

Part 1 :
Part 2 :
// ==================================================
// > PART 1
// ==================================================
$solution_1 = $input->lines->map(function ($line) {
    return $line->split(" | ")[1]->split(" ")
        ->map(fn ($w) => $w->chars()->count())
        ->keep([2, 4, 3, 7])->count();
})->sum();

// ==================================================
// > PART 2
// ==================================================
$solution_2 = $input->lines->map(function ($line) {
    [$input, $output] = $line->split(" | ");
    $input = $input->split(" ")->map(fn ($i) => $i->chars()->sort()->join(""));

    // Solve by comparing numbers of common display
    $resolutions_rules = [
        2 => 1,
        3 => 7,
        4 => 4,
        7 => 8,
        5 => [[3, 1, 2], [2, 4, 2], [5, 4, 3]],
        6 => [[6, 7, 2], [0, 4, 3], [9, 4, 4]]
    ];

    $nums = [];
    foreach ($resolutions_rules as $nd=>$rule) {
        $respos = [];
        foreach ($input as $i) {
            if (strlen($i) != $nd) continue;

            // Direct resolution, because only one possibility
            if (!is_array($rule)) {
                $nums[$rule] = $i;
                continue 2;
            }

            $respos[] = $i;
        }

        // Several possibilities : check rule's expectations
        foreach ($rule as [$num, $comp, $expect]) {
            foreach ($respos as $irp=>$rp) {
                if (count(array_intersect(str_split($rp), str_split($nums[$comp]))) == $expect) {
                    $nums[$num] = $rp;
                    unset($respos[$irp]);
                    continue 2;
                }
            }
        }
    }
    $nums = set($nums)->map(fn ($s) => $s->string)->flip();

    // Translate output
    return $output->split(" ")
        ->map(fn ($i) => $nums[(string) $i->chars()->sort()->join("")])
        ->join("")->int;
})->sum();