11 bool __attribute__((format(printf
, 1, 2)))
12 debug(const char *fmt
, ...) {
14 if (getenv("DEBUG")) {
16 vfprintf(stderr
, fmt
, ap
);
32 static bool used
[LIMIT
+ 1];
34 int main(int argc
, char **argv
) {
35 size_t len
= 0, count
= 0;
39 /* Part 1 - read data */
41 if (!(stdin
= freopen(argv
[1], "r", stdin
))) {
46 while (getline(&line
, &len
, stdin
) >= 0) {
48 fprintf(stderr
, "recompile with larger LIMIT!\n");
51 if (sscanf(line
, "%d,%d,%d,%d\n", &array
[count
].x
, &array
[count
].y
,
52 &array
[count
].z
, &array
[count
].t
) != 4) {
53 fprintf(stderr
, "bad input\n");
58 printf("Read %zu lines\n", count
);
62 for (i
= 1; i
< count
; i
++) {
63 for (j
= 0; j
< i
; j
++) {
64 if (abs(array
[i
].x
- array
[j
].x
) + abs(array
[i
].y
- array
[j
].y
)
65 + abs(array
[i
].z
- array
[j
].z
) + abs(array
[i
].t
- array
[j
].t
) <= 3) {
66 debug("point at index %d near index %d\n", j
, i
);
68 if (array
[i
].c
!= array
[j
].c
) {
71 for (k
= 0; k
< i
; k
++)
73 array
[k
].c
= array
[i
].c
;
78 array
[i
].c
= array
[j
].c
;
83 for (j
= 1; j
<= i
+ 1; j
++)
92 for (i
= 1; i
< count
+ 1; i
++)
95 printf("Found %d constellations\n", j
);