implement day 5 with hashmaps to gain a 20x speed increase
[advent21.git] / 05a.c
1 #include <stdio.h>
2 #include <uthash.h>
3
4 #define SWAP(x, y) { x ^= y; y ^= x; x ^= y; }
5
6 struct point { int x; int y; };
7 struct entry { struct point key; int i; UT_hash_handle hh; };
8
9 void mark_point(int x, int y, struct entry **entries, int *r)
10 {
11 struct entry *p;
12 struct point s = { .x=x, .y=y };
13 HASH_FIND(hh, *entries, &s, sizeof(struct point), p);
14 if (p) {
15 if (p->i++ == 1)
16 *r = *r+1;
17 } else {
18 p = malloc(sizeof(struct entry));
19 p->key = s;
20 p->i = 1;
21 HASH_ADD(hh, *entries, key, sizeof(struct point), p);
22 }
23 }
24
25 int main()
26 {
27 char buf[1000];
28 struct entry *entries = NULL;
29 int r = 0;
30 while (fgets(buf, 1000, stdin) != NULL) {
31 char *ptr = &buf[0];
32 int x1 = strtol(ptr, &ptr, 10);
33 ptr++;
34 int y1 = strtol(ptr, &ptr, 10);
35 ptr+=4;
36 int x2 = strtol(ptr, &ptr, 10);
37 ptr++;
38 int y2 = strtol(ptr, &ptr, 10);
39 if (x1 > x2)
40 SWAP(x1, x2);
41 if (y1 > y2)
42 SWAP(y1, y2);
43
44 //Vertical
45 if (x1 == x2)
46 for (int y = y1; y<=y2; y++)
47 mark_point(x1, y, &entries, &r);
48 //Horizontal
49 else if (y1 == y2)
50 for (int x = x1; x<=x2; x++)
51 mark_point(x, y1, &entries, &r);
52 }
53 printf("%d\n", r);
54 }