blob: 74779478cf45b107b40084bef1ec77988486e7c3 (
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
|
#!/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
|