minus-squarelandreville@lemmy.worldtoAdvent Of Code@programming.dev•💃 - 2025 DAY 6 SOLUTIONS - 💃linkfedilinkarrow-up2·5 天前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 linkfedilink
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