-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08.go
45 lines (43 loc) · 1.07 KB
/
08.go
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
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
scanner := bufio.NewScanner(os.Stdin)
grid := []string{}
for scanner.Scan() {
grid = append(grid, scanner.Text())
}
n := len(grid[0])*2 + len(grid)*2 - 4 // trees on edges
highest := 0
for i := 1; i < len(grid[0])-1; i++ {
for j := 1; j < len(grid)-1; j++ {
score, k, visible := 1, 0, false
for k = i - 1; k > 0 && grid[j][k] < grid[j][i]; k-- {
}
visible = visible || k == 0 && grid[j][k] < grid[j][i]
score *= (i - k)
for k = i + 1; k < len(grid[j])-1 && grid[j][k] < grid[j][i]; k++ {
}
visible = visible || k == len(grid[j])-1 && grid[j][k] < grid[j][i]
score *= (k - i)
for k = j - 1; k > 0 && grid[k][i] < grid[j][i]; k-- {
}
visible = visible || k == 0 && grid[k][i] < grid[j][i]
score *= (j - k)
for k = j + 1; k < len(grid)-1 && grid[k][i] < grid[j][i]; k++ {
}
visible = visible || k == len(grid)-1 && grid[k][i] < grid[j][i]
score *= (k - j)
if score > highest {
highest = score
}
if visible {
n += 1
}
}
}
fmt.Println(n, highest)
}