21
|
1 #! /usr/bin/env ruby
|
|
2
|
|
3 if ARGV.length != 1
|
|
4 abort("Incorrect arguments - needs input file")
|
|
5 elsif not File.exist? (ARGV[0])
|
|
6 abort("File #{ARGV[0]} did not exist")
|
|
7 end
|
|
8
|
|
9 file = ARGV[0]
|
|
10
|
|
11 platform = File.open(file, "r").each_line(chomp: true).map {|row| row.each_char.to_a}.to_a.transpose()
|
|
12
|
|
13 COL_SIZE = platform[0].length
|
|
14
|
|
15 chunked_platform = platform.map {|col| col.chunk_while {|i, j| i != "#"}.to_a}
|
|
16 total_load = chunked_platform.reduce(0) do |weight, col|
|
|
17 _, col_weight = col.reduce([COL_SIZE, 0]) do |accum, chunk|
|
|
18 first_obj_weight, temp_weight = accum
|
|
19 num_moved_objs = chunk.count("O")
|
|
20 last_obj_weight = first_obj_weight - num_moved_objs
|
|
21 temp_weight += ((first_obj_weight * (first_obj_weight + 1)) / 2) - ((last_obj_weight * (last_obj_weight + 1)) / 2)
|
|
22 first_obj_weight -= chunk.length
|
|
23 [first_obj_weight, temp_weight]
|
|
24 end
|
|
25 weight + col_weight
|
|
26 end
|
|
27 puts total_load |