59 lines
1.1 KiB
PHP
59 lines
1.1 KiB
PHP
|
<?php
|
||
|
|
||
|
[$min, $max] = explode('-', file_get_contents('input.txt'));
|
||
|
|
||
|
/**
|
||
|
* @param int|string $big_number
|
||
|
*
|
||
|
* @return bool
|
||
|
*/
|
||
|
function has_at_least_two_adjacent_number($big_number)
|
||
|
{
|
||
|
$big_number = (string) $big_number;
|
||
|
$current_number = null;
|
||
|
for ($i = 0, $l = strlen($big_number); $i < $l; $i++) {
|
||
|
if ($current_number === $big_number[$i]) {
|
||
|
return true;
|
||
|
}
|
||
|
$current_number = $big_number[$i];
|
||
|
}
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param int|string $big_number
|
||
|
* @return bool
|
||
|
*/
|
||
|
function never_decrease($big_number)
|
||
|
{
|
||
|
$big_number = (string) $big_number;
|
||
|
$current_number = null;
|
||
|
for ($i = 0, $l = strlen($big_number); $i < $l; $i++) {
|
||
|
if ($current_number > $big_number[$i]) {
|
||
|
return false;
|
||
|
}
|
||
|
$current_number = $big_number[$i];
|
||
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param int|string $number
|
||
|
*
|
||
|
* @return bool
|
||
|
*/
|
||
|
function is_valid($number)
|
||
|
{
|
||
|
return has_at_least_two_adjacent_number($number) && never_decrease($number);
|
||
|
}
|
||
|
|
||
|
$valids = [];
|
||
|
for ($i = $min; $i <= $max; $i++) {
|
||
|
if (is_valid($i)) {
|
||
|
$valids[] = $i;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
echo count($valids);
|