Day 6: Trash Compactor

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

you are viewing a single comment's thread
view the rest of the comments
[โ€“] 2 points 10 months ago

Ruby

I decided to rotate the entire input character-by-character, then parse the numbers (see the full source here)

grid = input.lines.map(&:chomp).map {|l| l.each_char.map.to_a }.to_a
transposed = Array.new(grid[0].length) { Array.new(grid.length) }

grid.each_with_index do |row, y|
  row.each_with_index do |col, x|
    transposed[x][y] = col
  end
end

vals = []
ops = []
temp_vals = []

transposed.each do |row|
  l = row.join("").strip
  temp_vals << l.scan(/\d+/).map(&:to_i).to_a[0]
  /[+*]/.match(l) { |m| ops << m.to_s.to_sym }

  if l == ""
    vals << temp_vals.compact
    temp_vals = []
  end
end

vals << temp_vals.compact unless temp_vals.empty?

vals.each_with_index.sum do |v, i|
  v.inject(ops[i])
end
  • source