summaryrefslogtreecommitdiff
path: root/8.c
blob: a400462881904f18acba7b2a841fa1587a19a6f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>


// Check if a tree is newly seen, by being taller than max or not
//
// If it is seen, update max.
//
// If the tree is seen, and has not been seen before, return 1
// Otherwise return 0
int
tree_seen(int * max, unsigned char * tree)
{
	int height = (*tree & 0x7F) - '0';
	int seen = 0;

	if (height > *max) {
		if (!(*tree & 0x80u)) {
			*tree |= 0x80u;
			++seen;
		}
		*max = height;
	}

	return seen;
}


// Check the number of seen trees in a forest
int
forest_seen(unsigned char * forest, int rows, int cols)
{
	int i, j, max, seen = 0;

	// For each row...
	for (i = 0; i < rows; ++i) {
		// Check which trees can be newly seen from the left
		for (j = 0, max = -1; j < cols - 1 && max < 9; ++j) {
			seen += tree_seen(&max, forest + i * cols + j);
		}

		// Check which trees can be newly seen from the right
		for (j = cols - 2, max = -1; j >= 0 && max < 9; --j) {
			seen += tree_seen(&max, forest + i * cols + j);
		}
	}

	// For each column...
	for (j = 0; j < cols - 1; ++j) {
		// Check which trees can be newly seen from the top
		for (i = 0, max = -1; i < rows && max < 9; ++i) {
			seen += tree_seen(&max, forest + i * cols + j);
		}

		// Check which trees can be newly seen from the bottom
		for (i = rows - 1, max = -1; i >= 0 && max < 9; --i) {
			seen += tree_seen(&max, forest + i * cols + j);
		}
	}

	return seen;
}


// Get the maximum scenic score for a tree
int
forest_maxscore(unsigned char * forest, int rows, int cols)
{
	int i, j, max = 0;

	for (i = 1; i < rows - 1; ++i) {
		for (j = 1; j < cols - 2; ++j) {
			int dist;
			int score = 1;
			int height = forest[i * cols + j];

			for (dist = 1; j - dist > 0; ++dist)
				if (forest[i * cols + j - dist] >= height)
					break;
			score *= dist;

			for (dist = 1; j + dist < cols - 2; ++dist)
				if (forest[i * cols + j + dist] >= height)
					break;
			score *= dist;

			for (dist = 1; i - dist > 0; ++dist)
				if (forest[(i - dist) * cols + j] >= height)
					break;
			score *= dist;

			for (dist = 1; i + dist < rows - 1; ++dist)
				if (forest[(i + dist) * cols + j] >= height)
					break;
			score *= dist;

			if (score > max)
				max = score;
		}
	}

	return max;
}


int
main(int argc, char ** argv)
{
	int i, part = 1;
	int cols = 0, rows = 0;
	char * forest;

	while ((i = getopt(argc, argv, "p:")) != -1) {
		switch (i) {
		case 'p':
			part = atoi(optarg);
			break;

		default:
			return -1;
		}
	}

	// Read first line
	forest = malloc(BUFSIZ);
	if (!fgets(forest, BUFSIZ, stdin)) {
		fprintf(stderr, "Need more input!\n");
		return -1;
	}
	cols = strlen(forest);
	if (cols < 2 || forest[cols - 1] != '\n') {
		fprintf(stderr, "Unexpected first line! (length %d)\n", cols);
		return -1;
	}
	++rows;

	while (1) {
		// Resize forest for next line.
		// Do the dumb thing and rely on realloc() to overallocate and manage
		// exponential growth of the underlying buffer
		//
		// If we were expecting huge input, we could just ask for a filename and
		// mmap() it.
		char * p = realloc(forest, (long) cols * (rows + 1) + 1);
		if (!p) {
			fprintf(stderr, "Realloc %ld fail! (%s)\n",
					(long) cols * (rows + 1) + 2, strerror(errno));
			free(forest);
			return -1;
		}
		forest = p;

		// Read line.
		p = forest + cols * rows;
		if (!fgets(p, cols + 1, stdin))
			break;

		// Check line length
		if (strlen(p) != cols || p[cols - 1] != '\n') {
			fprintf(stderr, "Unexpected line length for %s\n", p);
			free(forest);
			return -1;
		}

		++rows;
	}

	switch (part) {
	case 1:
		printf("Trees seen: %d\n",
				forest_seen((unsigned char *) forest, rows, cols));
		break;

	case 2:
		printf("Best visibility: %d\n",
				forest_maxscore((unsigned char *) forest, rows, cols));
		break;

	default:
		fprintf(stderr, "Unexpected part %d\n", part);
		free(forest);
		return -1;
	}

	free(forest);

	return 0;
}