2022 day 18 fix POSIX
[aoc_eblake.git] / 2019 / day4.c
blob76d5e2612404390eae7c7b4146e8d9dd95d1e9f8
1 #define _GNU_SOURCE 1
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <stdarg.h>
6 #include <stdbool.h>
7 #include <limits.h>
9 static int do_debug = -1;
10 void debug(const char *fmt, ...) {
11 va_list ap;
12 if (do_debug < 0)
13 do_debug = !!getenv("DEBUG");
14 if (do_debug > 0) {
15 va_start(ap, fmt);
16 vfprintf(stderr, fmt, ap);
17 va_end(ap);
21 void check(int i, int *a, int *b) {
22 char buf[8];
23 char count[10] = "";
24 int j;
25 bool pair = false;
27 if (snprintf(buf, sizeof buf, "0%d", i) != 7) {
28 printf("invalid input %d\n", i);
29 exit(1);
31 for (j = 1; j < 7; j++) {
32 if (buf[j] < buf[j-1])
33 return;
34 count[buf[j] - '0']++;
36 for (j = 0; j < 10; j++) {
37 if (count[j] > 1) {
38 pair = true;
39 if (count[j] == 2) {
40 ++*b;
41 break;
45 if (pair)
46 ++*a;
49 int main(int argc, char **argv) {
50 int low = 158126, high = 624574;
51 int i, count1 = 0, count2 = 0, possible = 0;
53 if (argc > 1)
54 high = low = atoi(argv[1]);
55 if (argc > 2)
56 high = atoi(argv[2]);
58 for (i = low; i <= high; i++, possible++)
59 check(i, &count1, &count2);
60 printf("%d / %d possibilities over %d in range\n", count1, count2, possible);
61 return 0;