day 14 optimize again
[aoc_eblake.git] / 2018 / day3a.c
blob2d6361d686b7e06b0611e5648bc9a658b48b8838
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 char array[LIMIT][LIMIT];
20 static void process(const char *str) {
21 int a, b, c, d, e;
22 int i, j;
23 if (sscanf(str, "#%d @ %d,%d: %dx%d\n", &a, &b, &c, &d, &e) != 5) {
24 printf("bad input!\n");
25 exit(1);
27 if (b + d > LIMIT || c + e > LIMIT) {
28 printf("recompile with larger LIMIT!\n");
29 exit(1);
31 debug("processing %d,%d: %dx%d\n", b, c, d, e);
32 for (i = b; i < b + d; i++)
33 for (j = c; j < c + e; j++)
34 array[j][i]++;
37 int main(int argc, char **argv) {
38 size_t len = 0, count = 0;
39 char *line;
40 int i, j;
42 if (argc > 1)
43 if (!(stdin = freopen(argv[1], "r", stdin))) {
44 perror("failure");
45 exit(2);
48 while (getline(&line, &len, stdin) >= 0) {
49 count++;
50 process(line);
52 printf("read %zd lines\n", count);
53 count = 0;
54 for (i = 0; i < LIMIT; i++)
55 for (j = 0; j < LIMIT; j++) {
56 debug("%d %d %d\n", i, j, array[j][i]);
57 if (array[j][i] > 1)
58 count++;
60 printf("found %zd overlaps\n", count);
61 return 0;