2022 day 21 fix POSIX
[aoc_eblake.git] / 2018 / day2b.c
blob1dff881ae0284696dd41ac107e64b8f4c2b6b513
1 #define _GNU_SOURCE 1
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <stdarg.h>
7 void debug(const char *fmt, ...) {
8 va_list ap;
9 if (getenv("DEBUG")) {
10 va_start(ap, fmt);
11 vfprintf(stderr, fmt, ap);
12 va_end(ap);
16 static void compare(const char *a, const char *b) {
17 const char *p = a, *q = b;
18 int diff = 0;
19 while (*p) {
20 if (*p != *q)
21 diff++;
22 p++;
23 q++;
25 debug(" comparing %s and %s: %d\n", a, b, diff);
26 if (diff == 1) {
27 printf("Nearby: %s"
28 " %s", a, b);
29 exit(0);
33 int main(int argc, char **argv) {
34 char **list = NULL;
35 size_t size = 0, len = 0;
36 char *line;
37 size_t i, j;
39 if (argc > 1)
40 stdin = freopen(argv[1], "r", stdin);
42 while (getline(&line, &len, stdin) >= 0) {
43 list = reallocarray(list, ++size, sizeof *list);
44 list[size - 1] = strdup(line);
46 printf("read %zd lines\n", size);
47 for (i = 0; i < size; i++)
48 for (j = i + 1; j < size; j++)
49 compare(list[i], list[j]);
50 printf("no nearby found\n");
51 return 1;