commit 3a18add1a8675056d1ce9cf706876a5e17f3c2ba
parent 60bfcfb33293320a139d24e57b6c4867784b6e09
Author: bsandro <email@bsandro.tech>
Date: Tue, 2 Dec 2025 21:22:56 +0200
day02 p2 alternative - no strings comparison, pure numerical solution
Diffstat:
| M | day02.c | | | 23 | +++++++++++++++-------- |
1 file changed, 15 insertions(+), 8 deletions(-)
diff --git a/day02.c b/day02.c
@@ -5,6 +5,9 @@
#include <stdbool.h>
#include "cputime.h"
+static const uint64_t pow10[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
+ 100000000, 1000000000, 10000000000, 100000000000, 1000000000000};
+
typedef struct Range {
uint64_t first;
uint64_t last;
@@ -41,16 +44,20 @@ bool is_silly1(uint64_t num) {
}
bool is_silly2(uint64_t num) {
- int d = digits(num);
- char s[16];
- itoa(num, s);
- for (int i=1;i<=d/2;++i) { // checking pattern 0-i
+ unsigned int d = digits(num);
+ for (unsigned int i=1;i<=d/2;++i) { // checking pattern 0-i
if (d%i!=0) continue;
- int rep = 0;
- for (int j=0;j<d;j+=i) {
- rep |= strncmp(s, s+j, i);
+ uint64_t stride = pow10[i];
+ uint64_t block = num%stride; //first block (right)
+ bool funny = true;
+ for (unsigned int j=0;j<d/i;++j) {
+ uint64_t jj = pow10[i*j];
+ if ((num/jj)%stride!=block) {
+ funny = false;
+ break;
+ }
}
- if (rep==0) return true;
+ if (funny) return true;
}
return false;
}