diff options
Diffstat (limited to '2024/07b/run.rb')
-rwxr-xr-x | 2024/07b/run.rb | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/2024/07b/run.rb b/2024/07b/run.rb new file mode 100755 index 0000000..7477947 --- /dev/null +++ b/2024/07b/run.rb @@ -0,0 +1,68 @@ +#!/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_with_index do |start_value| + OPERATIONS.each do |operation| + if(operation == :"||") + test_result = (start_value.to_s + vals[index].to_s).to_i + else + test_result = start_value.public_send(operation, value) + end + + 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 + + + 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 +p result.sum + |