aoc-2019/day4/1/secure_container.c

53 lines
932 B
C
Raw Normal View History

2019-12-04 23:35:51 +00:00
#include "neat_macro.h"
int p_six_digit(char* s) {
int counter = 0;
for(char* c=s; *c!='\0'; c++) {
counter++;
}
return (counter == 6);
}
int p_double(char *s) {
char last_char = '\0';
for (char *c = s; *c!='\0'; c++) {
2019-12-04 23:51:19 +00:00
if(last_char == *c) return 1;
2019-12-04 23:35:51 +00:00
last_char = *c;
}
return 0;
}
int p_decrease(char *s) {
char last_char = '\0';
for (char *c = s; *c!='\0'; c++) {
if ((last_char - '0') > (*c - '0')) return 0;
last_char = *c;
}
return 1;
}
int main(int argc, char **argv) {
int start = atoi(argv[1]);
int end = atoi(argv[2]);
int count = 0;
for(int i=start; i<end; i++){
char buf[7];
snprintf(buf, 7, "%d", i);
if ((i % 100000)==0) {
log("At %d/%d %f%%\n", i, end, (((double)i) / end) * 100);
log("At %s\n", buf);
}
if(p_six_digit(buf) && p_double(buf) && p_decrease(buf)){
count++;
}
}
printf("Numbers: %d\n", count);
}