File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change
1
+ require_relative 'day'
2
+
3
+ class Day07 < Day # >
4
+
5
+ # @example
6
+ # day.part1 #=> 3749
7
+ def part1
8
+ input
9
+ . split ( "\n " )
10
+ . map { |line | line . scan ( /\d +/ ) . map ( &:to_i ) }
11
+ . map { |nums | [ nums [ 0 ] , nums [ 1 ..-1 ] ] }
12
+ . select { |test_value , operands |
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
+ }
20
+ }
21
+ . any? { |result | result == test_value }
22
+ }
23
+ . map { |test_value , _ | test_value }
24
+ . reduce ( &:+ )
25
+ end
26
+
27
+ # @example
28
+ # day.part2 #=> 'how are you'
29
+ def part2
30
+ end
31
+
32
+ EXAMPLE_INPUT = File . read ( "../inputs/day07-example-input.txt" )
33
+ end
You can’t perform that action at this time.
0 commit comments