} } // bah humbug return false; } // is it nice? function is_nice($string) { // rule 1 if (!has_a_two_letter_pair($string)) { return false; } // rule 2 if (!has_a_pair_with_one_char_between($string)) { return false; } return true; } // load strings into array $data = file('input/day-5.txt'); // count nice strings $nice = 0; // loop through strings foreach ($data as $key => $str) { // if string is "nice", increment counter if (is_nice($str)) { $nice++; } } // output answer echo 'Answer: ' . $nice;
function testIncrementPostfixExpressionOnFunctionPostfix($param) { return is_nice($param)++; }
} } foreach ($cantMatchCountMap as $pattern) { if (DEBUG) { echo "\tTest NOT: {$pattern}: "; } $count = preg_match_all($pattern, $string); if ($count > 0) { if (DEBUG) { echo "Failed", PHP_EOL; } return false; } if (DEBUG) { echo "Passed", PHP_EOL; } } return true; } // tests assert(is_nice('qjhvhtzxzqqjkmpb')); assert(is_nice('xxyxx')); assert(!is_nice('uurcxstgmygtbstg')); assert(!is_nice('ieodomkazucvgmuy')); $contents = []; $contents = file('input.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); $niceCount = 0; foreach ($contents as $potential) { is_nice($potential) ? ++$niceCount : null; } echo "{$niceCount} nice strings", PHP_EOL;
return true; } } return false; } function has_pairs($string) { $chars = str_split($string); foreach ($chars as $i => $char) { if (isset($chars[$i + 1]) && strpos($string, $char . $chars[$i + 1], $i + 2) !== false) { return true; } } return false; } function is_nice2($string) { return has_pairs($string) && has_groups($string); } $lines = file(__DIR__ . '/input/5'); $lines = array_map('trim', $lines); $count = 0; $count2 = 0; foreach ($lines as $line) { $count += (int) is_nice($line); $count2 += (int) is_nice2($line); } echo "part 1: "; var_Dump($count); echo "part 2: "; var_Dump($count2);