input = DATA.read .scan(/\d+/).map(&:to_i) .each_slice(2).to_a.transpose .map(&:sort) left, right = input # part one pp left.zip(right).sum { (_1 - _2).abs } # part two pp left.sum { _1 * right.count(_1) } __END__ 3 4 4 3 2 5 1 3 3 9 3 3