-
Notifications
You must be signed in to change notification settings - Fork 0
/
day17.rb
413 lines (342 loc) · 9.44 KB
/
day17.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
require_relative 'toolbox'
# plan:
# [X] keep list of traveled nodes
# [X] loop through list of nodes
# [X] check if have valid moves
# [X] if valid moves found, choose the south most one and do it
# [X] no moves? game over
# [X] after move made, fill troughs
# [X] columns filled are removed from the list of nodes
class Grid
def initialize(coordinates)
x_min = coordinates.map(&:first).min
x_max = coordinates.map(&:first).max
@y_min = coordinates.map(&:last).min
@y_max = coordinates.map(&:last).max
# +1 for the array, then +2 for 1 square overflow on either side
width = (x_max - x_min) + 1 + 2
height = @y_max + 1
offset = x_min - 1
LOGGER.debug { "x_min: #{x_min}, x_max: #{x_max}, y_max: #{@y_max}, width: #{width}" }
@grid = Array.new(width) { Array.new(height, :sand) }
coordinates.each do |x, y|
x_trans = x - offset
@grid[x_trans][y] = :clay
end
@spring_x, @spring_y = 500 - offset, 0
@grid[@spring_x][@spring_y] = :spring
@grid_backup = @grid.dup
end
def spring
[@spring_x, @spring_y]
end
def render
(0...@grid.first.size).each do |i|
puts @grid.map {|n| _display(n[i]) }.flatten.join(' ')
end
end
def at(coordinate)
x, y = *coordinate
return nil unless 0 <= x && x < @grid.size
return nil unless 0 <= y && y < @grid.first.size
@grid[x][y]
end
def set(coordinate, type)
x, y = *coordinate
@grid[x][y] = type
end
def water_count
count = 0
@grid.each_with_index do |col, x|
col.each_with_index do |square, y|
next if y < @y_min
next if @y_max < y
count += 1 if square == :water || square == :column
end
end
count
end
def remaining_water_count
count = 0
@grid.each_with_index do |col, x|
col.each_with_index do |square, y|
next if y < @y_min
next if @y_max < y
count += 1 if square == :water
end
end
count
end
def insights
total_squares = @grid.size * @grid.first.size
total_water = @grid.map {|col| col.select {|s| s ==:water }.size }.reduce(:+)
total_columns = @grid.map {|col| col.select {|s| s ==:column }.size }.reduce(:+)
explored_squares = total_water + total_columns
puts "explored: #{((explored_squares.to_f / total_squares)*100).round(0)}% water: #{total_water} columns: #{total_columns}"
end
def save_run
File.open('data/day17_last_run.txt', 'w') do |file|
(0...@grid.first.size).each do |i|
file.puts(@grid.map {|n| _display(n[i]) }.flatten.join(' '))
end
end
end
private
def _display(x)
case x
when :sand
return '.'
when :clay
return '#'
when :column
return '|'
when :water
return '~'
when :spring
return '+'
when nil
return 'E'
else
LOGGER.fatal { "encountered unknown x: #{x}" }
exit
end
end
end
class Water
def initialize(grid, current_coordinate = nil)
@grid = grid
@nodes = []
@nodes << @grid.spring
end
def flow(n = -1)
filled_troughs_previously = false
iter = 0
water_tiles = 0
loop do
break if iter == n
@grid.insights if iter != 0 && iter % 1_000 == 0
LOGGER.debug { "iter: #{iter}" }
flowable_nodes = @nodes.select {|n| _has_move?(n) }
LOGGER.debug { "flow nodes: #{flowable_nodes}" }
if flowable_nodes.empty?
# try to fill troughs
waters = _fill_troughs
# no flow, and no filling, we are done
break if waters.empty?
# water is static. remove it from candidate nodes
waters.each do |c|
@nodes.delete(c)
end
next
end
flowable_nodes.each do |flow_node|
valid_directions = _valid_directions(flow_node)
loop do
break if valid_directions.empty?
flow_direction = valid_directions.shift
node = _move(flow_node, flow_direction)
@nodes << node
end
end
iter += 1
end
@grid.insights
end
private
def _move(coordinate, direction)
n = _coordinate_for(direction, coordinate)
@grid.set(n, :column)
n
end
def _has_move?(from_coordinate)
!_valid_directions(from_coordinate).empty?
end
def _valid_directions(from_coordinate)
[:south, :west, :east].select {|d| _can_move?(from_coordinate, d) }
end
def _can_move?(from_coordinate, direction)
south = _square_in_direction(:south, from_coordinate)
case direction
when :south
return south == :sand
when :west
west = _square_in_direction(:west, from_coordinate)
return (west == :sand) && (!south.nil? && (south == :clay || south == :water))
when :east
east = _square_in_direction(:east, from_coordinate)
return (east == :sand) && (!south.nil? && (south == :clay || south == :water))
else
return false
end
end
def _square_in_direction(direction, from_coordinate)
@grid.at(_coordinate_for(direction, from_coordinate))
end
def _fill_troughs
waters = []
candidates = @nodes.dup
loop do
LOGGER.debug { "candidates: #{candidates.inspect}" }
break if candidates.empty?
coordinate = candidates.shift
LOGGER.debug { "checking coordinate: #{coordinate.inspect}" }
x, y = *coordinate
potential_trough = true
w_x = x - 1
w_y = y
loop do
western_object = @grid.at([w_x, w_y])
case western_object
when :column
w_x = w_x - 1
next
when nil
potential_trough = false
break
when :sand
potential_trough = false
break
when :water
LOGGER.fatal { "encountered unexpected water at #{coordinate}" }
@grid.render
exit
when :clay
break
else
LOGGER.fatal { "encountered unknown square heading west at #{w_x}, #{w_y}: #{western_object}" }
@grid.render
exit
end
end
next unless potential_trough
LOGGER.debug { "found sufficient western expansion" }
# now go east
e_x = x + 1
e_y = y
loop do
eastern_object = @grid.at([e_x, e_y])
case eastern_object
when :column
e_x = e_x + 1
next
when nil
potential_trough = false
break
when :sand
potential_trough = false
break
when :water
LOGGER.fatal { "encountered unexpected water at #{coordinate}" }
@grid.render
exit
when :clay
break
else
LOGGER.fatal { "encountered unknown square heading east at #{e_x}, #{e_y}: #{eastern_object}" }
@grid.render
exit
end
end
next unless potential_trough
LOGGER.debug { "found sufficient eastern expansion" }
# now check that its held up by clay or water and account for the walls
trough_start = w_x + 1
trough_end = e_x - 1
LOGGER.debug { "checking from #{trough_start}..#{trough_end}" }
(trough_start..trough_end).each do |c_x|
c_y = y + 1
bottom_coordinate = [c_x, c_y]
bottom = @grid.at(bottom_coordinate)
LOGGER.debug { "bottom_coordinate: #{bottom} at #{bottom_coordinate}" }
unless (bottom == :clay || bottom == :water)
potential_trough = false
break
end
end
# fill!
if potential_trough
LOGGER.debug { "ground support for trough found" }
(trough_start..trough_end).each do |c_x|
@grid.set([c_x, y], :water)
candidates.delete([c_x, y])
waters << [c_x, y]
end
else
LOGGER.debug { "no ground support for trough" }
end
end
waters
end
def _coordinate_for(direction, starting_coordinate)
x, y = *starting_coordinate
return [ x, y + 1] if direction == :south
return [x - 1, y] if direction == :west
return [x + 1, y] if direction == :east
return [x , y - 1] if direction == :north
LOGGER.fatal { "tried to move in bad direction: #{direction}" }
exit
end
end
def input_data(f)
input_data = []
raw_lines(f).each do |line|
# extract numbers
axis, start, finish = *line.scan(/\d+/).map(&:to_i)
(start..finish).each do |v|
input_data << (line.chars.index('x') == 0 ? [axis, v] : [v, axis])
end
end
input_data
end
def run_tests
puts "testing"
data = input_data('data/day17_test_scan.txt')
grid = Grid.new(data)
water = Water.new(grid)
water.flow
test(57, grid.water_count)
data = [
[495, 1],
[495, 2],
[495, 3],
[495, 4],
[495, 5],
[495, 6],
[499, 2],
[499, 3],
[500, 4],
[501, 2],
[501, 3],
[505, 1],
[505, 2],
[505, 3],
[505, 4],
[505, 5],
[505, 6],
]
grid = Grid.new(data)
water = Water.new(grid)
water.flow
test(17, grid.water_count)
puts
end
def setup_grid
puts "setting up grid:"
data = input_data('data/day17_production_scan.txt')
grid = Grid.new(data)
water = Water.new(grid)
water.flow
grid
end
def part1(grid)
grid.water_count
end
def part2(grid)
grid.remaining_water_count
end
run_tests
grid = setup_grid
puts "Part 1: How many tiles can the water reach within the range of y values in your scan?"
puts "Answer: #{part1(grid)}"
puts "Part 2: How many water tiles are left after the water spring stops producing water and all remaining water not at rest has drained?"
puts "Answer: #{part2(grid)}"