aboutsummaryrefslogtreecommitdiff
path: root/day08/part2.rb
blob: fa4a61c250a5f4c78d87c0cbfd50177986ca4204 (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
def score_up(grid, ri, ci)
  tree = grid[ri][ci]
  vis = 0
  loop do
    ri -= 1
    break if ri < 0
    vis += 1
    break if grid[ri][ci] >= tree
  end
  vis
end

def score_down(grid, ri, ci)
  tree = grid[ri][ci]
  vis = 0
  loop do
    ri += 1
    break if ri >= grid.count
    vis += 1
    break if grid[ri][ci] >= tree
  end
  vis
end

def score_left(grid, ri, ci)
  tree = grid[ri][ci]
  vis = 0
  loop do
    ci -= 1
    break if ci < 0
    vis += 1
    break if grid[ri][ci] >= tree
  end
  vis
end

def score_right(grid, ri, ci)
  tree = grid[ri][ci]
  vis = 0
  loop do
    ci += 1
    break if ci >= grid[ri].count
    vis += 1
    break if grid[ri][ci] >= tree
  end
  vis
end

grid = $stdin.readlines.map(&:strip).map(&:chars).map { |row| row.map(&:to_i) }

max_score = 0

grid.count.times do |ri|
  grid[ri].count.times do |ci|
    score = 1
    score *= score_up(grid, ri, ci)
    score *= score_down(grid, ri, ci)
    score *= score_left(grid, ri, ci)
    score *= score_right(grid, ri, ci)
    max_score = score if score > max_score
  end
end

puts max_score