day 4 golf again
[aoc_eblake.git] / 2018 / day3b.c
blob70147d47fac05d21a43b896de9b8c33523574ae2
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 #define LIMIT 1000
18 short array[LIMIT][LIMIT];
19 char list[1500];
21 static void process(const char *str) {
22 int a, b, c, d, e;
23 int i, j;
24 if (sscanf(str, "#%d @ %d,%d: %dx%d\n", &a, &b, &c, &d, &e) != 5) {
25 printf("bad input!\n");
26 exit(1);
28 if (b + d > LIMIT || c + e > LIMIT) {
29 printf("recompile with larger LIMIT!\n");
30 exit(1);
32 debug("processing #%d %d,%d: %dx%d\n", a, b, c, d, e);
33 for (i = b; i < b + d; i++)
34 for (j = c; j < c + e; j++) {
35 if (array[j][i])
36 list[a] = list[array[j][i]] = 1;
37 else
38 array[j][i] = a;
42 int main(int argc, char **argv) {
43 size_t len = 0, count = 0;
44 char *line;
45 int i, j;
47 if (argc > 1)
48 if (!(stdin = freopen(argv[1], "r", stdin))) {
49 perror("failure");
50 exit(2);
53 while (getline(&line, &len, stdin) >= 0) {
54 count++;
55 process(line);
57 printf("read %zd lines\n", count);
58 count = 0;
59 for (i = 0; i < LIMIT; i++)
60 for (j = 0; j < LIMIT; j++) {
61 debug("%d %d %d\n", i, j, array[j][i]);
62 if (array[j][i] > 1)
63 count++;
65 printf("found %zd overlaps\n", count);
66 for (i = 1; i < sizeof list; i++)
67 if (!list[i])
68 break;
69 printf("unique claim %d\n", i);
70 return 0;