Example #1
0
function nextValidPass($pass)
{
    do {
        $pass = nextPass($pass);
    } while (!isValid($pass));
    return $pass;
}
Example #2
0
<?php

$input = file_get_contents('day11.txt');
$ans1 = nextPass($input);
printf('ans#11.1: %s' . PHP_EOL, $ans1);
$ans2 = nextPass($ans1);
printf('ans#11.2: %s' . PHP_EOL, $ans2);
function nextPass($input)
{
    while (true) {
        $input++;
        $chars = str_split($input);
        if (array_intersect(['i', 'o', 'l'], $chars)) {
            continue;
        }
        $seq = false;
        $pairs = 0;
        $skip = 0;
        for ($j = 1; $j < count($chars); $j++) {
            if ($skip !== $j && $chars[$j] === $chars[$j - 1]) {
                $pairs++;
                $skip = $j + 1;
            }
            if (!$seq && $j >= 2) {
                $one = $chars[$j - 2];
                $two = $chars[$j - 1];
                $three = $chars[$j];
                if (ord($one) + 2 === ord($three) && ord($two) + 1 === ord($three)) {
                    $seq = true;
                }
            }
Example #3
0
    $len = count($chars) - 2;
    for ($i = 0; $i < $len; $i++) {
        if (ord($chars[$i]) == ord($chars[$i + 1]) - 1 && ord($chars[$i]) == ord($chars[$i + 2]) - 2) {
            return true;
        }
    }
    return false;
}
function hasPairs($chars)
{
    $len = count($chars);
    for ($i = 0; $i < $len - 3; $i++) {
        if ($chars[$i] == $chars[$i + 1]) {
            for ($j = $i + 2; $j < $len; $j++) {
                if ($chars[$i] != $chars[$j] && $chars[$j] == $chars[$j + 1]) {
                    return true;
                }
            }
        }
    }
    return false;
}
$pass = '******';
//part1
$pass = '******';
//part2
do {
    $pass = nextPass($pass);
    //print ("${pass}\n");
} while (!isValid($pass));
print "{$pass}\n";