Skip to content

Instantly share code, notes, and snippets.

@coryrose1
Created July 30, 2020 18:55
Show Gist options
  • Save coryrose1/c271e6b73f1d5dd89e5800734425197d to your computer and use it in GitHub Desktop.
Save coryrose1/c271e6b73f1d5dd89e5800734425197d to your computer and use it in GitHub Desktop.
<?php
namespace App\Services;
use App\Match;
use App\Range;
class Digits
{
public static function calculate()
{
ini_set('max_execution_time', 560);
set_time_limit(0);
$ranges = Range::where('done', 0)->take(5)->get();
foreach ($ranges as $range) {
$validPasscodes = 0;
for ($firstNumber = $range->start; $firstNumber <= $range->end; $firstNumber++) {
// Number is a 6 digit numeric passcode
if ($firstNumber > 999999) {
continue;
}
// check for repeating digits
// check for decreasing integer
$repeatingDigits = 0;
$decreasingDigits = 0;
$digits = str_split($firstNumber);
for ($i = 0; $i < strlen($firstNumber) - 1; $i++) {
$x = $i + 1;
if ((int)$digits[$i] == (int)$digits[$x]) {
$repeatingDigits++;
}
if ((int)$digits[$i] > (int)$digits[$x]) {
$decreasingDigits++;
}
}
if ($repeatingDigits == 0 || $decreasingDigits !== 0) {
continue;
}
$validPasscodes++;
Match::create([
'range_id' => $range->id,
'match' => $firstNumber
]);
}
$range->num_valid = $validPasscodes;
$range->done = 1;
$range->save();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment