#!/usr/bin/env ruby input = File.readlines("1.input").map(&:split) first = input.map(&:first).compact last = input.map(&:last).compact result = first.map do |v| v.to_i * last.select { _1.match?(v) }.size end.sum puts result