-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9_b.rb
127 lines (102 loc) · 2.85 KB
/
9_b.rb
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
# typed: true
# frozen_string_literal: true
class Level
class << self
def run(input)
map = Map.new
map.seed(input)
puts "Output: #{map.analyze}"
end
end
end
class Map
extend T::Sig
sig { returns(T::Array[T::Array[Integer]]) }
attr_accessor :map
sig { returns(T::Array[T::Array[Integer]]) }
attr_accessor :basins
sig { returns(T::Array[Integer]) }
attr_accessor :low_points
sig { returns(Integer) }
attr_accessor :max_x
sig { returns(Integer) }
attr_accessor :max_y
sig { void }
def initialize
@map = [[0]]
@low_points = []
@basins = []
end
sig { params(input: String).returns(T.self_type) }
def seed(input)
@map = input.split("\n").map { |line| line.chars.map(&:to_i) }
@max_x = @map.length
@max_y = T.must(@map.first).length
self
end
sig { returns(Integer) }
def analyze
find_low_points
find_basins
analyze_largest_basins
end
sig { returns(Integer) }
def analyze_low_points
find_low_points
@low_points.map { |(x, y)| @map[x][y] }.sum + @low_points.length
end
sig { void }
def find_low_points
@low_points = []
@map.each_with_index do |row, x|
row.each_with_index do |cur, y|
next if cur == 9
adjacent = analyze_adjacent(x, y)
next unless T.must(adjacent[:lower]).none? && T.must(adjacent[:higher]).any?
@low_points << [x, y]
end
end
end
sig { void }
def find_basins
@basins = @low_points.map do |(x, y)|
calc_basin(x, y).uniq
end
@basins = @basins.sort_by(&:length).reverse
end
sig { returns(Integer) }
def analyze_largest_basins
@basins[0..2].map(&:length).inject(1) { |acc, basin_size| acc * basin_size }
end
# rubocop:disable Naming/MethodParameterName
sig { params(x: Integer, y: Integer).returns(T::Array[T::Array[Integer]]) }
def calc_basin(x, y)
adjacent = T.must(analyze_adjacent(x, y)[:higher])
results = T.let([], T::Array[T::Array[Integer]])
results << [x, y] unless @map[x][y] == 9
adjacent.each do |(adj_x, adj_y)|
adjacent_result = calc_basin(T.must(adj_x), T.must(adj_y))
next if adjacent_result.none?
results += adjacent_result
end
results
end
sig { params(x: Integer, y: Integer).returns(T::Hash[Symbol, T::Array[T::Array[Integer]]]) }
def analyze_adjacent(x, y)
val = @map[x][y]
result = { lower: [], higher: [] }
[[x - 1, y], [x + 1, y], [x, y - 1], [x, y + 1]].each do |(new_x, new_y)|
next unless valid_coord?(new_x, new_y)
cur = @map.dig(new_x, new_y)
next unless cur
key = val < cur ? :higher : :lower
result[key] << [new_x, new_y]
end
result
end
sig { params(x: Integer, y: Integer).returns(T::Boolean) }
def valid_coord?(x, y)
x <= @max_x && x >= 0 && y <= @max_y && y >= 0
end
# rubocop:enable Naming/MethodParameterName
end