Skip to content

Commit 8b887ad

Browse files
committed
day 7, a skosh faster
39 seconds -> 21 seconds, so not *fast*. Rediscovered Enumberable#find/detect to return the first truthy eval of a block.
1 parent 23e5403 commit 8b887ad

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

2024/ruby/day07.rb

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ def part1
1111
.map { |nums| [ nums[0] , nums[1..-1] ] }
1212
.select { |test_value, operands|
1313
operator_combos = [:+, :*].repeated_permutation(operands.length - 1)
14-
operator_combos.map { |combo|
15-
combo
16-
.each_with_index
17-
.reduce(operands[0]) { |result, (op, idx)|
18-
result = [ result, operands[idx+1] ].reduce(op)
19-
}
14+
operator_combos.find { |combo|
15+
test_value ==
16+
combo
17+
.each_with_index
18+
.reduce(operands[0]) { |result, (op, idx)|
19+
result = [ result, operands[idx+1] ].reduce(op)
20+
}
2021
}
21-
.any? { |result| result == test_value }
2222
}
2323
.map { |test_value, _| test_value }
2424
.reduce(&:+)
@@ -33,14 +33,14 @@ def part2
3333
.map { |nums| [ nums[0] , nums[1..-1] ] }
3434
.select { |test_value, operands|
3535
operator_combos = [:+, :*, :concat].repeated_permutation(operands.length - 1)
36-
operator_combos.map { |combo|
37-
combo
38-
.each_with_index
39-
.reduce(operands[0]) { |result, (op, idx)|
40-
result = [ result, operands[idx+1] ].reduce(op)
41-
}
36+
operator_combos.find { |combo|
37+
test_value ==
38+
combo
39+
.each_with_index
40+
.reduce(operands[0]) { |result, (op, idx)|
41+
result = [ result, operands[idx+1] ].reduce(op)
42+
}
4243
}
43-
.any? { |result| result == test_value }
4444
}
4545
.map { |test_value, _| test_value }
4646
.reduce(&:+)

0 commit comments

Comments
 (0)