43 lines
1.2 KiB
C
43 lines
1.2 KiB
C
#include <stdio.h>
|
|
|
|
#define MAXW 255
|
|
|
|
char heightmap[MAXW][MAXW];
|
|
int iw, ih; // actual width/height of input, for scalability. assume rectangular input
|
|
|
|
int checktree(int x0, int y0, int dx, int dy, int *oob) {
|
|
int x, y, d = 0;
|
|
for (x = x0+dx, y = y0+dy; x < iw && x >= 0 && y < ih && y >= 0; x += dx, y += dy, d++)
|
|
if (heightmap[y][x] >= heightmap[y0][x0])
|
|
return d + 1; // higher tree in dir, return distance + 1 cuz it sees tree
|
|
*oob = 1;
|
|
return d; // no higher tree in dir
|
|
}
|
|
|
|
int main()
|
|
{
|
|
int x, y;
|
|
int oob; // used to keep track if a check went out of bounds
|
|
int numtrees = 0, score = 0, maxscore = 0;
|
|
|
|
for (; ih < MAXW && fgets(heightmap[ih], MAXW, stdin); ih++) ; // calculate height of grid and populate heightmap at same time
|
|
for (; heightmap[0][iw] != '\n'; iw++) ; // calculate width of lines
|
|
|
|
for (y = 0; y < ih; y++) {
|
|
for (x = 0; x < iw; x++) {
|
|
oob = 0;
|
|
// check tree in all directions
|
|
score = checktree(x, y, 1, 0, &oob);
|
|
score *= checktree(x, y, -1, 0, &oob);
|
|
score *= checktree(x, y, 0, 1, &oob);
|
|
score *= checktree(x, y, 0, -1, &oob);
|
|
if (score > maxscore)
|
|
maxscore = score;
|
|
if (oob)
|
|
numtrees++;
|
|
}
|
|
}
|
|
printf("p1: %d\np2:%d\n", numtrees, maxscore);
|
|
return 0;
|
|
}
|