<?php declare(strict_types=1); use Illuminate\\Support\\Collection; \$input = <<<\'PHP\' 47|53 97|13 97|61 97|47 75|29 61|13 75|53 29|13 97|29 53|29 61|53 97|53 61|29 47|13 75|47 97|75 47|61 75|61 47|29 75|13 53|13 75,47,61,53,29 97,61,53,29,13 75,29,13 75,97,47,61,53 61,13,29 97,13,75,29,47 PHP; [\$rules, \$updates] = explode(\"\\n\\n\", \$input); \$rules = Collection::make(array_map(static fn (string \$rule): Rule => Rule::fromNotation(\$rule), explode(\"\\n\", \$rules))); \$updates = Collection::make(array_map(static fn (string \$update): Update => Update::fromNotation(\$update), explode(\"\\n\", \$updates))); return \$updates ->filter(static fn (Update \$update): bool => \$update->validate(\$rules)) ->map(static fn (Update \$update): int => \$update->middle()) ->sum(); final readonly class Rule \{ public function __construct( public int \$before, public int \$after, ) \{\} public function appliesTo(Update \$update): bool \{ \$hasBefore = \\in_array(\$this->before, \$update->pages, true); \$hasAfter = \\in_array(\$this->after, \$update->pages, true); return \$hasBefore && \$hasAfter; \} public function validate(Update \$update): bool \{ \$beforeIndex = array_search(\$this->before, \$update->pages, true); \$afterIndex = array_search(\$this->after, \$update->pages, true); return \$beforeIndex <= \$afterIndex; \} public static function fromNotation(string \$notation): self \{ [\$left, \$right] = explode(\'|\', \$notation, 2); return new self((int) \$left, (int) \$right); \} \} final readonly class Update \{ /** * @param int[] \$pages */ public function __construct( public array \$pages, ) \{\} public function middle(): int \{ return \$this->pages[(int) floor(\\count(\$this->pages) / 2)]; \} /** * @param Collection<array-key, Rule> \$rules */ public function validate(Collection \$rules): bool \{ return \$rules->every(fn (Rule \$rule): bool => ! \$rule->appliesTo(\$this) || \$rule->validate(\$this)); \} public static function fromNotation(string \$notation): self \{ return new self(array_map(\'intval\', explode(\',\', \$notation))); \} \}