<?php declare(strict_types=1); \$input = <<<\'PHP\' xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5)) PHP; final readonly class Computer \{ public function process(string \$input): int \{ return array_reduce( \$this->getInstructions(\$input), static fn (\$result, \$instruction) => \$result + \$instruction->calculate(), 0, ); \} /** * @return array<array-key, Instruction> */ private function getInstructions(string \$input): array \{ \$matches = []; preg_match_all(\'/mul\\(\\d+,\\d+\\)/\', \$input, \$matches); return array_map( static fn (string \$instruction) => new Instruction(\$instruction), \$matches[0], ); \} \} final readonly class Instruction \{ public function __construct( private string \$value, ) \{\} public function calculate(): int \{ \$matches = []; preg_match_all(\'/\\d+/\', \$this->value, \$matches); [\$left, \$right] = \$matches[0]; return (int) \$left * (int) \$right; \} \} \$computer = new Computer(); \$computer->process(\$input);