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
|
def check_up(grid, ri, ci)
tree = grid[ri][ci]
loop do
ri -= 1
break if ri < 0
return false unless grid[ri][ci] < tree
end
true
end
def check_down(grid, ri, ci)
tree = grid[ri][ci]
loop do
ri += 1
break if ri >= grid.count
return false unless grid[ri][ci] < tree
end
true
end
def check_left(grid, ri, ci)
tree = grid[ri][ci]
loop do
ci -= 1
break if ci < 0
return false unless grid[ri][ci] < tree
end
true
end
def check_right(grid, ri, ci)
tree = grid[ri][ci]
loop do
ci += 1
break if ci >= grid[ri].count
return false unless grid[ri][ci] < tree
end
true
end
grid = $stdin.readlines.map(&:strip).map(&:chars).map { |row| row.map(&:to_i) }
num_visible = 0
grid.count.times do |ri|
grid[ri].count.times do |ci|
visible = false
visible = true if check_up(grid, ri, ci)
visible = true if check_down(grid, ri, ci)
visible = true if check_left(grid, ri, ci)
visible = true if check_right(grid, ri, ci)
num_visible += 1 if visible
end
end
puts num_visible
|