diff options
Diffstat (limited to '2024/07a/run.rb')
-rwxr-xr-x | 2024/07a/run.rb | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/2024/07a/run.rb b/2024/07a/run.rb new file mode 100755 index 0000000..0192174 --- /dev/null +++ b/2024/07a/run.rb @@ -0,0 +1,72 @@ +#!/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 + |