when rule 4 violated, skip to next possible number (99.7% savings)

This commit is contained in:
2022-09-20 21:30:39 +02:00
parent a461bf2842
commit edf09a53af
2 changed files with 42 additions and 14 deletions

View File

@@ -18,17 +18,44 @@
#include <getopt.h>
#include "debug.h"
#include "likely.h"
static int is_valid(int number, int part)
/**
* next_number() - finds next suitable number after a faulty right digit
* @number: the number
* @faulty: the faulty digit position (1, 10, 100, etc...)
* @left: the left digit of faulty one
*
* This function is called when rule 4 is violated:
* "Going from left to right, the digits never decrease."
* Example: 453456
* ^
* Here we will replace the faulty digit and next ones with its left digit
* value (5 here). Returned number will be 455555.
* This function allowed to keep 546,495/548,022 calls (99.7% savings !)
*/
static int next_number(int number, int faulty, int left)
{
int valid = 0, dups[10] = { 0 };
int digit, dec;
int next = number - number % (faulty * 10);
for (digit = number % 10; number > 10; digit = dec) {
for (; faulty; faulty /= 10)
next += left * faulty;
return next;
}
static int is_valid(int number, int part, int *next)
{
int valid = 0, dups[10] = { 0 }, work = number;
int digit, dec = number + 1, faulty = 1;
*next = number + 1;
for (digit = number % 10; number > 10; digit = dec, faulty *= 10) {
number /= 10;
dec = number % 10;
if (dec > digit)
if (dec > digit) {
*next = next_number(work, faulty, dec);
return 0;
}
if (dec == digit) {
valid = 1;
dups[digit] += 2;
@@ -44,13 +71,12 @@ static int is_valid(int number, int part)
static int doit(int *nums, int part)
{
int res = 0;
int res = 0, next = 0;
/* There is surely a way to avoid 99% of useless calls to is_valid.
*/
for (int i = nums[0]; i < nums[1]; ++i)
if (is_valid(i, part))
for (int i = nums[0]; i < nums[1]; i = next) {
if (unlikely(is_valid(i, part, &next)))
res++;
}
return res;
}
@@ -92,6 +118,8 @@ int main(int ac, char **av)
if (optind < ac)
return usage(*av);
//next_number(154456, 1000, 5);
//exit (0);
parse(nums);
printf("%s : res=%d\n", *av, doit(nums, part));