<?php declare(strict_types=1); \$input = <<<\'PHP\' 190: 10 19 3267: 81 40 27 83: 17 5 156: 15 6 7290: 6 8 6 15 161011: 16 10 13 192: 17 8 14 21037: 9 7 18 13 292: 11 6 16 20 PHP; return array_sum(array_map( static fn (Equation \$equation): int => \$equation->testValue, array_filter( array_map( static fn (string \$notation): Equation => Equation::create(\$notation), explode(\"\\n\", \$input), ), static fn (Equation \$equation): bool => \$equation->valid(), ), )); final readonly class Equation \{ /** * @param int[] \$numbers */ public function __construct( public int \$testValue, public array \$numbers, ) \{\} public function valid(bool \$part2 = true): bool \{ return \$this->process(\$this->numbers, \$part2); \} public static function create(string \$input): self \{ preg_match_all(\'/\\d+/\', \$input, \$matches); \$numbers = array_map(\'intval\', \$matches[0]); \$testValue = array_shift(\$numbers); return new self(\$testValue, \$numbers); \} private function process(array \$numbers, bool \$part2): bool \{ if (\\count(\$numbers) === 1) \{ return \$numbers[0] === \$this->testValue; \} \$left = array_shift(\$numbers); \$right = array_shift(\$numbers); if (\$left > \$this->testValue) \{ return false; \} return \$this->process([\$left * \$right, ...\$numbers], \$part2) || \$this->process([\$left + \$right, ...\$numbers], \$part2) || (\$part2 && \$this->process([(int) \"\{\$left\}\{\$right\}\", ...\$numbers], true)); \} \}