🎉 Hello world
This commit is contained in:
59
day_4/part_1.php
Normal file
59
day_4/part_1.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?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);
|
Reference in New Issue
Block a user