= $min && $letters[$key] <= $max) return 1; return 0; } return parse_passwords_and_solve($passwords, 'solve_1'); } function solve_part_two(array $passwords): int { function solve_2(int $pos1, int $pos2, string $key, string $pass): int { if ($pass[$pos1 - 1] === $key xor $pass[$pos2 - 1] === $key) return 1; return 0; } return parse_passwords_and_solve($passwords, 'solve_2'); } function parse_passwords_and_solve(array $passwords, callable $fun): int { $ret = 0; foreach ($passwords as $policy) { preg_match('/^(\d+?)-(\d+?) ([a-z]): (.+?)$/', $policy, $matches); [$match, $min, $max, $key, $pass] = $matches; if ($match) $ret += $fun($min, $max, $key, $pass); } return $ret; }