#!/usr/bin/env ruby data = File.read("data.txt") data = data.split("\n") data = data.map(&:split) first_row = data.map { |data| data[0] }.sort second_row = data.map { |data| data[1] }.sort merged = first_row.zip(second_row) distance = merged.map do |row| (row[0].to_i - row[1].to_i).abs end.sum p distance