9 void debug(const char *fmt
, ...) {
11 if (getenv("DEBUG")) {
13 vfprintf(stderr
, fmt
, ap
);
30 static int8_t level(int x
, int y
, int s
) {
32 int p
= (id
* y
+ s
) * id
;
33 return (p
% 1000) / 100 - 5;
36 static void rows(int x
, int y
) {
37 grid
[x
][y
].row
[0] = grid
[x
][y
].value
;
38 for (int i
= 1; i
< LIMIT
- y
; i
++)
39 grid
[x
][y
].row
[i
] = grid
[x
][y
].row
[i
- 1] + grid
[x
][y
+ i
].value
;
42 static void find(int size
) {
45 best
[size
- 1].sum
= -5 * size
* size
;
46 for (j
= 0; j
< LIMIT
- size
+ 1; j
++) {
48 for (i
= 0; i
< size
; i
++)
49 sum
+= grid
[i
][j
].row
[size
- 1];
50 for (i
= 0; i
< LIMIT
- size
+ 1; i
++) {
52 sum
+= grid
[i
+ size
- 1][j
].row
[size
- 1] -
53 grid
[i
- 1][j
].row
[size
- 1];
54 if (sum
> best
[size
- 1].sum
) {
55 best
[size
- 1].sum
= sum
;
56 best
[size
- 1].x
= i
+ 1;
57 best
[size
- 1].y
= j
+ 1;
59 } else if (sum
== best
[size
- 1].sum
)
64 debug("ignoring duplicate best candidates for size %d\n", size
);
67 int main(int argc
, char **argv
) {
74 for (j
= 0; j
< LIMIT
; j
++)
75 for (i
= 0; i
< LIMIT
; i
++)
76 grid
[i
][j
].value
= level(i
+ 1, j
+ 1, s
);
77 for (j
= 0; j
< LIMIT
; j
++)
78 for (i
= 0; i
< LIMIT
; i
++)
80 for (i
= 0; i
< LIMIT
; i
++) {
83 if (best
[i
].sum
> best
[max
].sum
)
85 else if (best
[i
].sum
== best
[max
].sum
)
86 debug("size %d has same best as %d\n", i
, max
);
88 printf("best at %d,%d,%d with %d\n", best
[max
].x
, best
[max
].y
, max
+ 1,