File tree Expand file tree Collapse file tree 1 file changed +26
-1
lines changed Expand file tree Collapse file tree 1 file changed +26
-1
lines changed Original file line number Diff line number Diff line change @@ -25,9 +25,34 @@ def part1
25
25
end
26
26
27
27
# @example
28
- # day.part2 #=> 'how are you'
28
+ # day.part2 #=> 11387
29
29
def part2
30
+ input
31
+ . split ( "\n " )
32
+ . map { |line | line . scan ( /\d +/ ) . map ( &:to_i ) }
33
+ . map { |nums | [ nums [ 0 ] , nums [ 1 ..-1 ] ] }
34
+ . select { |test_value , operands |
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
+ }
42
+ }
43
+ . any? { |result | result == test_value }
44
+ }
45
+ . map { |test_value , _ | test_value }
46
+ . reduce ( &:+ )
30
47
end
31
48
32
49
EXAMPLE_INPUT = File . read ( "../inputs/day07-example-input.txt" )
33
50
end
51
+
52
+ class Integer
53
+ # @example
54
+ # 12.concat(345) #=> 12345
55
+ def concat ( other )
56
+ "#{ self } #{ other } " . to_i
57
+ end
58
+ end
You can’t perform that action at this time.
0 commit comments