#!/usr/bin/env ruby data = File.read("data.txt") data = data.split("\n") data = data.map(&:split) first_row = data.map { |data| data[0].to_i } second_row = {} data.map do |data| value = data[1].to_i count = second_row[value] ? second_row[value] + 1 : 1 second_row[value] = count end similiarity = first_row.map do |value| multiplayer = second_row.fetch(value, 0) value * multiplayer end.sum p similiarity