9 void debug(const char *fmt
, ...) {
11 if (getenv("DEBUG")) {
13 vfprintf(stderr
, fmt
, ap
);
27 void display(int count
, int y
, int minx
, int maxx
) {
29 for (i
= minx
; i
<= maxx
; i
++) {
31 for (j
= 0; j
< count
; j
++)
32 if (list
[j
].y
== y
&& list
[j
].x
== i
)
39 int main(int argc
, char **argv
) {
40 size_t len
= 0, count
= 0;
43 int minx
, miny
, maxx
, maxy
, gap
;
46 /* Part 1 - read in lines */
48 if (!(stdin
= freopen(argv
[1], "r", stdin
))) {
53 while (getline(&line
, &len
, stdin
) >= 0) {
54 if (sscanf(line
, "position=<%d,%d> velocity=<%d,%d> ", &list
[count
].x
,
55 &list
[count
].y
, &list
[count
].vx
, &list
[count
].vy
) != 4) {
56 fprintf(stderr
, "bad input\n");
61 printf("Read %zu lines\n", count
);
63 /* Part 2 - minimize gap */
65 maxx
= maxy
= -100000;
67 for (i
= 0; i
< count
; i
++) {
77 printf("Starting with gap %d, column %d\n", maxy
- miny
, minx
);
82 maxx
= maxy
= -100000;
83 for (i
= 0; i
< count
; i
++) {
84 list
[i
].x
+= list
[i
].vx
;
85 list
[i
].y
+= list
[i
].vy
;
95 } while (maxy
- miny
< gap
);
96 printf("After %d iters, gap is %d, column %d\n", iters
, gap
, minx
);
98 /* Part 3 - undo last iteration, and print array */
99 for (i
= 0; i
< count
; i
++) {
100 list
[i
].x
-= list
[i
].vx
;
101 list
[i
].y
-= list
[i
].vy
;
103 for (i
= miny
- 2; i
< maxy
+ 2; i
++)
104 display(count
, i
, minx
- 3, maxx
+ 3);