@@ -11,14 +11,14 @@ def part1
11
11
. map { |nums | [ nums [ 0 ] , nums [ 1 ..-1 ] ] }
12
12
. select { |test_value , operands |
13
13
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
+ }
20
21
}
21
- . any? { |result | result == test_value }
22
22
}
23
23
. map { |test_value , _ | test_value }
24
24
. reduce ( &:+ )
@@ -33,14 +33,14 @@ def part2
33
33
. map { |nums | [ nums [ 0 ] , nums [ 1 ..-1 ] ] }
34
34
. select { |test_value , operands |
35
35
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
+ }
42
43
}
43
- . any? { |result | result == test_value }
44
44
}
45
45
. map { |test_value , _ | test_value }
46
46
. reduce ( &:+ )
0 commit comments