summaryrefslogtreecommitdiff
path: root/2024/07a/run.rb
blob: 0192174b09cd2386616ee8253095fc2cc45ed7a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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