summary refs log tree commit diff homepage
path: root/2022/day08.awk
blob: 3ce1a50586d644b39e042cf022b774f96c1a5f02 (plain) (blame)
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
function visible(x, y) {
	if (x == 1 || x == right) return 1;
	if (y == 1 || y == bottom) return 1;
	for (xx = x-1; xx >= 1; --xx) {
		if (m[xx,y] >= m[x,y]) break;
	}
	if (!xx) return 1;
	for (xx = x+1; xx <= right; ++xx) {
		if (m[xx,y] >= m[x,y]) break;
	}
	if (xx > right) return 1;
	for (yy = y-1; yy >= 1; --yy) {
		if (m[x,yy] >= m[x,y]) break;
	}
	if (!yy) return 1;
	for (yy = y+1; yy <= bottom; ++yy) {
		if (m[x,yy] >= m[x,y]) break;
	}
	if (yy > bottom) return 1;
	return 0;
}
function view(x, y) {
	if (x == 1 || x == right) return 0;
	if (y == 1 || y == bottom) return 0;
	v = 1;
	for (xx = x-1; xx > 1; --xx) {
		if (m[xx,y] >= m[x,y]) break;
	}
	v *= x-xx;
	for (xx = x+1; xx < right; ++xx) {
		if (m[xx,y] >= m[x,y]) break;
	}
	v *= xx-x;
	for (yy = y-1; yy > 1; --yy) {
		if (m[x,yy] >= m[x,y]) break;
	}
	v *= y-yy;
	for (yy = y+1; yy < bottom; ++yy) {
		if (m[x,yy] >= m[x,y]) break;
	}
	v *= yy-y;
	return v;
}
BEGIN {
	FS = "";
}
{
	for (x = 1; x <= NF; ++x) {
		m[x,NR] = $x;
	}
	right = NF;
	bottom = NR;
}
END {
	for (x = 1; x <= right; ++x) {
		for (y = 1; y <= bottom; ++y) {
			if (visible(x, y)) vis++;
			if (view(x, y) > max) max = view(x, y);
		}
	}
	print vis;
	print max;
}