-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhitting_rock_bottom.rb
191 lines (162 loc) · 5.57 KB
/
hitting_rock_bottom.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
class HittingRockBottom
WATER_INDICATOR = "~"
ROCK_INDICATOR = "#"
AIR_INDICATOR = " "
def initialize(args)
@cave_map = parse_input_file(args)
@total_water_units = File.open(args).readline.to_i
@volume_of_each_column = initialize_volume
@current_water_position = find_initial_water_position
end
def pump_water
(1..@total_water_units).each do |water_unit|
place_water
end
check_and_mark_hanging_water
end
def print_map
puts @volume_of_each_column.inspect
@cave_map.each{|row| puts row.inspect + "\n"}
end
def print_output
output = ""
@volume_of_each_column.each do |volume_unit|
if volume_unit.is_a?(Integer)
output << "#{volume_unit} "
else
output << "~ "
end
end
puts output
end
private
def place_water
if flow_down?
puts "FLOW DOWN"
elsif flow_right?
puts "FLOW RIGHT"
elsif flow_up?
puts "FLOW UP"
else
puts "NO MOVE"
#TOP RIGHT
end
end
def flow_down?
return false if @current_water_position[:row] == @cave_map.length - 1 # NOTE: This assumes a gap in the # floor is possible
candidate_position = {:row => @current_water_position[:row] + 1, :column => @current_water_position[:column]}
if @cave_map[candidate_position[:row]][candidate_position[:column]] == AIR_INDICATOR
#mark the cave map
place_water_indicator(candidate_position)
#update current position
update_current_water_position(candidate_position)
#increment volume (we have :column here, so a array of values associated to each column should be simple)
increment_column_volume(candidate_position[:column])
return true
else
return false
end
end
def flow_right?
return false if @current_water_position[:column] == @cave_map.first.length - 1 # Far right
candidate_position = {:row => @current_water_position[:row], :column => @current_water_position[:column] + 1}
if @cave_map[candidate_position[:row]][candidate_position[:column]] == AIR_INDICATOR
#mark the cave map
place_water_indicator(candidate_position)
#update current position
update_current_water_position(candidate_position)
#increment volume
increment_column_volume(candidate_position[:column])
return true
else
return false
end
end
def flow_up?
#go up one, row, scan for last '~' and see if you can put one to the right, if not, go up again
#make sure we are not at the top of the cave
return false if @current_water_position[:row] == 0 #NOTE: This assumes a gap in the ceiling is possible
row_offset = 1
working = true
success = false
candidate_row_index = @current_water_position[:row] - row_offset
candidate_position = { :row => candidate_row_index,
:column => find_last_water_in_row(candidate_row_index) }
while working do
if @cave_map[candidate_position[:row]][candidate_position[:column]] == AIR_INDICATOR
#mark the cave map
place_water_indicator(candidate_position)
#update current position
update_current_water_position(candidate_position)
#increment volume
increment_column_volume(candidate_position[:column])
#break from while
success = true
working = false
else
#try next row up if possible
row_offset += 1
if @current_water_position[:row] - row_offset == 0
success = false
working = false
else
candidate_row_index = @current_water_position[:row] - row_offset
candidate_position = { :row => candidate_row_index,
:column => find_last_water_in_row(candidate_row_index) }
end
end
end
return success
end
def check_and_mark_hanging_water
#Check to see if we are mid-flow, if yes, turn that value to a string
if (@cave_map[@current_water_position[:row] + 1][@current_water_position[:column]]) == AIR_INDICATOR
@volume_of_each_column[@current_water_position[:column]] = @volume_of_each_column[@current_water_position[:column]].to_s
end
end
def find_last_water_in_row(row_index)
@cave_map[row_index].rindex(WATER_INDICATOR) + 1
end
def place_water_indicator(position)
@cave_map[position[:row]][position[:column]] = WATER_INDICATOR
end
def update_current_water_position(position)
@current_water_position[:row] = position[:row]
@current_water_position[:column] = position[:column]
end
def increment_column_volume(column)
@volume_of_each_column[column] += 1
end
def find_initial_water_position
water_position = {}
found_it = false
@cave_map.each_with_index do |map_row, row_index|
break if found_it
map_row.each_with_index do |map_row_item, item_index|
if map_row_item == WATER_INDICATOR
water_position = {:row => row_index, :column => item_index}
found_it = true
break
end
end
end
water_position
end
def initialize_volume
volume = [1] #This assumes we always start with one initial volume of water
(1...@cave_map.first.length).each{|column| volume << 0}
volume
end
def parse_input_file(input_file)
parsed_input_file = []
File.open(input_file).each_line do |line|
next unless line.start_with?(ROCK_INDICATOR) || line.start_with?(WATER_INDICATOR)
parsed_input_file << line.chomp.split(//)
end
parsed_input_file
end
end
test = HittingRockBottom.new("PuzzleNodeFiles/simple_cave.txt")
test.pump_water
test.print_map
test.print_output