@@ -18,34 +18,34 @@ class Day06 < Day # >
1818 def part1
1919 input
2020 . split ( "\n " )
21- . map { |line | line . scan ( /\S +/ ) }
22- . cheater_transpose
21+ . map { |line | line . scan ( /\S +/ ) } # 2D of human numbers, last array is operators (all still strings)
22+ . cheater_transpose # transposed, now each array represents a worksheet problem (column)
2323 . map { |problem |
24- operator = problem . pop
24+ operator = problem . pop # problem's operator appears as the last element of the problem array
2525 problem
26- . map ( &:to_i )
27- . reduce ( &operator . to_sym )
26+ . map ( &:to_i ) # string of human number to integer
27+ . reduce ( &operator . to_sym ) # solve this problem
2828 }
29- . reduce ( &:+ )
29+ . reduce ( &:+ ) # sum of all problem solutions
3030 end
3131
3232 # @example
3333 # day.part2 #=> 3263827
3434 def part2
3535 input
3636 . split ( "\n " )
37- . map { |line | line . chars }
38- . cheater_transpose
39- . chunk { _1 . all? " " } # mark empty columns as 'split'
40- . map { |split , problem |
41- next 0 if split
42- operator = problem . first . pop
37+ . map { |line | line . chars } # 2D of characters from the input
38+ . cheater_transpose # transposed, now each array is a column of characters from the worksheet
39+ . chunk { | col | col . all? " " } # group problem columns together by chunking on empties ...
40+ . map { |split , problem | # ... chunks that are empty are noted as split
41+ next 0 if split # skip the splits
42+ operator = problem . first . pop # problem's operator appears as the last character in the first column
4343 problem
44- . map ( &:join )
45- . map ( &:to_i )
46- . reduce ( &operator . to_sym )
44+ . map ( &:join ) # recreate a string of the digits (read vertically by the earlier transpose)
45+ . map ( &:to_i ) # make it a number (cephalopod number)
46+ . reduce ( &operator . to_sym ) # solve this problem
4747 }
48- . reduce ( &:+ )
48+ . reduce ( &:+ ) # sum of all problem solutions
4949 end
5050
5151 EXAMPLE_INPUT = File . read ( "../inputs/day06-example-input.txt" )
0 commit comments