-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07.rb
53 lines (41 loc) · 1.72 KB
/
07.rb
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
# https://adventofcode.com/2024/day/7
equations = File.readlines('input07.txt').map { |line| line.scan(/\d+/).map(&:to_i) }
########################################################################################################################
# 1 Both parts can be optimized by generation of the permutations manually, but it's good enough for the input
########################################################################################################################
def equation_can_be_true?(goal, operands)
['*', '+'].repeated_permutation(operands.length - 1).each do |ops|
result = operands[0]
ops.each_with_index do |op, index|
op == '*' ? result += operands[index + 1] : result *= operands[index + 1]
break if result > goal
end
return true if result == goal
end
false
end
puts equations.sum { |items| equation_can_be_true?(items[0], items[1..]) ? items[0] : 0 }
# 20281182715321
########################################################################################################################
# 2
########################################################################################################################
def equation_can_be_true2?(goal, operands)
['*', '+', '|'].repeated_permutation(operands.length - 1).each do |ops|
result = operands[0]
ops.each_with_index do |op, index|
case op
when '+'
result += operands[index + 1]
when '*'
result *= operands[index + 1]
when '|'
result = "#{result}#{operands[index + 1]}".to_i
end
break if result > goal
end
return true if result == goal
end
false
end
puts equations.sum { |items| equation_can_be_true2?(items[0], items[1..]) ? items[0] : 0 }
# 159490400628354