8 void debug(const char *fmt
, ...) {
10 if (getenv("DEBUG")) {
12 vfprintf(stderr
, fmt
, ap
);
21 unsigned char line
; /* non-zero only for coordinates in list */
22 unsigned char nearby
; /* 0 for unchecked or tie, else line id of nearest */
23 } array
[LIMIT
][LIMIT
];
29 } list
[LINES
]; /* line 0 unused */
30 static unsigned short minx
= LIMIT
, miny
= LIMIT
, maxx
= 0, maxy
= 0;
32 /* Return 0 to keep looking, non-zero for tie */
33 static int check(int x
, int y
, int *r
) {
34 if (x
< minx
|| x
> maxx
|| y
< miny
|| y
> maxy
)
36 if (array
[x
][y
].line
) {
38 debug("point %d,%d has at least two nearest neighbors\n", x
, y
);
41 *r
= array
[x
][y
].line
;
46 /* Return 0 for tie, else line of nearest coordinate */
47 static unsigned char compute(int x
, int y
) {
51 return array
[x
][y
].line
;
56 for (i
= 0; i
< d
; i
++) {
57 if (check(tryx
, tryy
, &r
))
62 for (i
= 0; i
< d
; i
++) {
63 if (check(tryx
, tryy
, &r
))
68 for (i
= 0; i
< d
; i
++) {
69 if (check(tryx
, tryy
, &r
))
74 for (i
= 0; i
< d
; i
++) {
75 if (check(tryx
, tryy
, &r
))
82 debug("no neighbors of %d,%d within %d\n", x
, y
, d
);
87 int main(int argc
, char **argv
) {
88 size_t len
= 0, count
= 0;
93 /* Part 1 - read in lines */
95 if (!(stdin
= freopen(argv
[1], "r", stdin
))) {
100 while (getline(&line
, &len
, stdin
) >= 0) {
102 if (count
>= LINES
) {
103 fprintf(stderr
, "recompile with larger LINES!\n");
106 if (sscanf(line
, "%d, %d ", &x
, &y
) != 2) {
107 fprintf(stderr
, "bad input\n");
110 if (x
> LIMIT
|| y
> LIMIT
) {
111 fprintf(stderr
, "recompile with larger LIMIT!\n");
116 array
[x
][y
].line
= count
;
126 printf("Read %zu lines, bounding box %d,%d to %d,%d\n", count
,
127 minx
, miny
, maxx
, maxy
);
133 /* Part 2 - compute nearest neighbors */
134 for (j
= miny
; j
<= maxy
; j
++)
135 for (i
= minx
; i
<= maxx
; i
++) {
136 unsigned char nearest
= compute(i
, j
);
138 array
[i
][j
].nearby
= nearest
;
139 list
[nearest
].count
++;
140 if (i
== minx
|| i
== maxx
|| j
== miny
|| j
== maxy
)
141 list
[nearest
].edge
= true;
145 /* Part 3 - determine non-edge point with most neighbors */
147 for (i
= 1; i
<= count
; i
++) {
148 debug("point %d,%d has %d neighbors\n", list
[i
].x
, list
[i
].y
,
152 if (list
[i
].count
> list
[best
].count
)
155 printf("best point %d,%d has %d neighbors\n", list
[best
].x
, list
[best
].y
,
158 /* Part 4 - determine number of points within 10000 of all list members */
160 for (j
= miny
; j
<= maxy
; j
++)
161 for (i
= minx
; i
<= maxx
; i
++) {
163 for (x
= 1; x
<= count
; x
++)
164 sum
+= abs(i
- list
[x
].x
) + abs(j
- list
[x
].y
);
165 debug("point %d,%d sum %d\n", i
, j
, sum
);
169 printf("found %d points within 10000\n", good
);