Advent of code 2023/9
Ajax Direct

Answer

Part 1 :
Part 2 :
$solution_1 = $solution_2 = 0;

foreach ($input->lines as $line) {
    $n = set([$line->numbers()]);
    $i = 0;
    while ($n[$i]->sum()) {
        $n[++$i] = set();
        for ($j = 1; $j < $n[$i - 1]->count(); $j++) {
            $n[$i][] = $n[$i-1][$j] - $n[$i-1][$j - 1];
        }
    }

    $n = $n->reverse();
    $n[0] = set([0, ...$n[0], 0]);
    for ($i = 1; $i < count($n); $i++) {
        $n[$i] = set([
            $n[$i]->first() - $n[$i-1]->first(),
            ...$n[$i],
            $n[$i]->last() + $n[$i-1]->last()
        ]);
    }

    $solution_1 += $n->last()->last();
    $solution_2 += $n->last()->first();
}