|
| 1 | +require_relative 'day' |
| 2 | +require_relative 'grid' |
| 3 | + |
| 4 | +class Day11 < Day # > |
| 5 | + |
| 6 | + # @example |
| 7 | + # day.part1 #=> 374 |
| 8 | + def part1 |
| 9 | + expanded_universe = |
| 10 | + Grid.new( |
| 11 | + expand_cosmically( |
| 12 | + scan(input) |
| 13 | + ) |
| 14 | + ) |
| 15 | + |
| 16 | + galaxies = [] |
| 17 | + expanded_universe.parse do |coords, value| |
| 18 | + galaxies << coords if value == GALAXY |
| 19 | + end |
| 20 | + |
| 21 | + galaxies |
| 22 | + .combination(2) |
| 23 | + .map { |here, there| expanded_universe.manhattan_distance(here, there) } |
| 24 | + .reduce(&:+) |
| 25 | + end |
| 26 | + |
| 27 | + # @example |
| 28 | + # day.part2 #=> 'how are you' |
| 29 | + def part2 |
| 30 | + end |
| 31 | + |
| 32 | + |
| 33 | + # @example |
| 34 | + # simple_input = "...\n.#.\n...\n" |
| 35 | + # day.scan(simple_input) #=> [[".",".","."],[".","#","."],[".",".","."]] |
| 36 | + def scan(image) |
| 37 | + image |
| 38 | + .split("\n") |
| 39 | + .map(&:chars) |
| 40 | + end |
| 41 | + |
| 42 | + EMPTY_SPACE = '.' |
| 43 | + GALAXY = '#' |
| 44 | + |
| 45 | + # @example |
| 46 | + # scanned_image = day.scan(EXAMPLE_INPUT) |
| 47 | + # input_expanded = day.expand_cosmically(scanned_image) |
| 48 | + # input_expanded #=> day.scan(EXPANDED_INPUT) |
| 49 | + def expand_cosmically(scanned_image) |
| 50 | + # dupe empty-space rows |
| 51 | + expanded_rows = [] |
| 52 | + scanned_image |
| 53 | + .each do |row| |
| 54 | + n = row.all?(EMPTY_SPACE) ? 2 : 1 |
| 55 | + n.times { expanded_rows << row } |
| 56 | + end |
| 57 | + |
| 58 | + # pivot the table, dupe empty-space columns |
| 59 | + expanded_columns = [] |
| 60 | + expanded_rows |
| 61 | + .transpose |
| 62 | + .each do |column| |
| 63 | + n = column.all?(EMPTY_SPACE) ? 2 : 1 |
| 64 | + n.times { expanded_columns << column } |
| 65 | + end |
| 66 | + |
| 67 | + # pivot things back around to original orientation |
| 68 | + # unnecessary for computing distances, but wanted for |
| 69 | + # visualizing output |
| 70 | + 3.times { expanded_columns = expanded_columns.transpose } |
| 71 | + expanded_columns |
| 72 | + end |
| 73 | + |
| 74 | + # @example simple input |
| 75 | + # simple_input = "...\n.#.\n...\n" |
| 76 | + # scanned_image = day.scan(simple_input) |
| 77 | + # scanned_image #=> [[".",".","."],[".","#","."],[".",".","."]] |
| 78 | + # day.display(scanned_image) #=> simple_input |
| 79 | + # @example example input |
| 80 | + # scanned_image = day.scan(EXAMPLE_INPUT) |
| 81 | + # day.display(scanned_image) #=> EXAMPLE_INPUT |
| 82 | + def display(scanned_image) |
| 83 | + scanned_image |
| 84 | + .map { |row| row.join('') } |
| 85 | + .join("\n") + "\n" |
| 86 | + end |
| 87 | + |
| 88 | + EXAMPLE_INPUT = <<~INPUT |
| 89 | + ...#...... |
| 90 | + .......#.. |
| 91 | + #......... |
| 92 | + .......... |
| 93 | + ......#... |
| 94 | + .#........ |
| 95 | + .........# |
| 96 | + .......... |
| 97 | + .......#.. |
| 98 | + #...#..... |
| 99 | + INPUT |
| 100 | + |
| 101 | + EXPANDED_INPUT = <<~INPUT |
| 102 | + ....#........ |
| 103 | + .........#... |
| 104 | + #............ |
| 105 | + ............. |
| 106 | + ............. |
| 107 | + ........#.... |
| 108 | + .#........... |
| 109 | + ............# |
| 110 | + ............. |
| 111 | + ............. |
| 112 | + .........#... |
| 113 | + #....#....... |
| 114 | + INPUT |
| 115 | +end |
0 commit comments