def reflection_points(pattern) (0.5...pattern.length-0.5).step(1).filter_map {|fold| a = pattern[0..fold.floor] b = pattern[fold.ceil..] if a.reverse.zip(b).reject { _1.any?(&:nil?) }.all? { _1 == _2 } fold else nil end }.map(&:ceil) end Pattern = Data.define(:pattern) do def reflection_axes ary = to_a reflection_points(ary).map { [:y, _1] } + reflection_points(ary.transpose).map { [:x, _1] } end def smudges return enum_for(__method__) unless block_given? max_y, max_x = extents (0..max_y).each do |y| (0..max_x).each do |x| smudged = case pattern[[y,x]] when ?# then ?. when ?. then ?# else fail end yield Pattern.new(pattern.merge({[y,x] => smudged})) end end end def to_a max_y, max_x = extents (0..max_y).map {|y| (0..max_x).map {|x| pattern.fetch([y,x]) } } end def to_s to_a.map(&:join).join("\n") end def extents max_y = pattern.keys.map(&:first).max max_x = pattern.keys.map(&:last).max [max_y, max_x] end end patterns = ARGF.read.strip.split("\n\n") .map {|pattern| pattern.split("\n").map(&:chars) .each.with_index.inject({}) {|h, (row, y)| h.merge(row.each.with_index.to_h {|elem,x| [[y,x], elem]}) } }.map { Pattern.new(_1) } # part one x, y = patterns.map { _1.reflection_axes.fetch(0) } .partition {|axis,_| axis == :x } .map { _1.map(&:last) } # keep index (discard axis) p x.sum + y.sum { _1 * 100 } # part two x, y = patterns.map {|pattern| original_reflection_axis = pattern.reflection_axes.fetch(0) pattern.smudges.lazy .flat_map { _1.reflection_axes} .reject { _1 == original_reflection_axis } .first }.partition {|axis,_| axis == :x } .map { _1.map(&:last) } # keep index (discard axis) p x.sum + y.sum { _1 * 100 }