#!/usr/bin/env ruby data = File.read("data.txt") valid = data.scan(/mul\(\d*,\d*\)/) sum = 0; ops = valid.map do |operation| operation.gsub(/[^\d*,]/, "").to_s.split(",").map(&:to_i).reduce(&:*) end p ops.sum