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