comparison day14.rb @ 21:46fb65f2cb94

Add Day 14 part 1 implementation If you look at columns and see "how far can this go" then the weight is just the sum of the partial triangle numbers of highest_weight to highest_weight minus number of rocks that can roll up to there. Part 2 seems to involve actually moving the rocks, though!
author IBBoard <dev@ibboard.co.uk>
date Sat, 16 Dec 2023 10:29:36 +0000
parents
children
comparison
equal deleted inserted replaced
20:fac484765bc9 21:46fb65f2cb94
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