<?php $input = \file_get_contents("inputs/11.txt"); function isValidPassword($password) { if (\strpos($password, "i") !== false || \strpos($password, "o") !== false || \strpos($password, "l") !== false || !\preg_match("/([a-z])\\1[a-z]*([a-z])\\2/", $password)) { return false; } for ($i = 0; $i < \strlen($password) - 2; $i++) { $a = $password[$i]; $b = $password[$i + 1]; $c = $password[$i + 2]; // We could do this with ord() and less variables, but PHP is neat, isnt it? :) if (++$a === $b && ++$a === $c) { return true; } } return false; } function getNextPassword($password) { while (!\isValidPassword(++$password)) { } return $password; } $nextPassword = \getNextPassword($input); $nextNextPassword = \getNextPassword($nextPassword); echo "The next password is `{$nextPassword}`, and after that its `{$nextNextPassword}`.";
// Check for forbidden characters if (preg_match('/[iol]/', $password)) { continue; } $sequence = false; $pairs = ['count' => 0, 'chars' => []]; for ($i = 1, $j = strlen($password); $i < $j; $i++) { if (!in_array($password[$i], $pairs['chars']) && $password[$i] === $password[$i - 1]) { // Matching pair $pairs['count']++; $pairs['chars'][] = $password[$i]; } if ($sequence === false && $i > 2) { $ordChar = ord($password[$i]); if (ord($password[$i - 2]) + 2 === $ordChar && ord($password[$i - 1]) + 1 === $ordChar) { $sequence = true; } } } // Found a password match? if (!$sequence || 2 !== $pairs['count']) { continue; } break; } return $password; } $input = 'cqjxjnds'; $newPassword = getNextPassword($input); echo 'New password is: ' . getNextPassword($newPassword) . PHP_EOL;
$password++; // Check for forbidden characters if (preg_match('/[iol]/', $password)) { continue; } $sequence = false; $pairs = ['count' => 0, 'chars' => []]; for ($i = 1, $j = strlen($password); $i < $j; $i++) { if (!in_array($password[$i], $pairs['chars']) && $password[$i] === $password[$i - 1]) { // Matching pair $pairs['count']++; $pairs['chars'][] = $password[$i]; } if ($sequence === false && $i > 2) { $ordChar = ord($password[$i]); if (ord($password[$i - 2]) + 2 === $ordChar && ord($password[$i - 1]) + 1 === $ordChar) { $sequence = true; } } } // Found a password match? if (!$sequence || 2 !== $pairs['count']) { continue; } break; } return $password; } $input = 'cqjxjnds'; echo 'New password is: ' . getNextPassword($input) . PHP_EOL;