#!/usr/bin/env ruby require 'pry' data = File.read("data.txt") OPERATIONS = %i(+ *) def test_combinations(test_value, vals) test_results = [vals.shift] all_vals_results = [] vals.each_with_index do |value, index| previous_results = test_results.clone test_results = [] previous_results.each do |start_value| OPERATIONS.each do |operation| test_result = start_value.public_send(operation, value) #p "test_results: " + test_results.to_s #p "previous: " + previous_results.to_s #p "value: " + value.to_s #p "operation "+ operation.to_s # #binding.pry if test_value == 3554112 # p previous_results if test_value == 3267 if test_result <= test_value && test_result > 0 test_results << test_result end if index + 1 == vals.size all_vals_results << test_result end end end end #binding.pry if test_value == 3554112 p all_vals_results all_vals_results.any? { |v| v == test_value } end result = data.split("\n").map do |row| vals = row.split ": " test_value = vals.shift.to_i vals = vals[0].split(" ").map(&:to_i) #if vals.sum < test_value # 0 #elsif vals.inject(&:*) > test_value # 0 #elsif vals.sum == test_value # test_value #elsif vals.inject(&:*) == test_value ## test_value #else if test_combinations(test_value, vals) test_value else 0 end #end end p result.sum