main
  1require "spec_helper"
  2
  3describe "problem eleven" do
  4=begin
  5In the 20×20 grid below, four numbers along a diagonal line have been marked in red.
  6
  7  |0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15 16 17 18 19
  8============================================================
  900|08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
 1001|49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
 1102|81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
 1203|52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
 1304|22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
 1405|24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
 1506|32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
 1607|67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
 1708|24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
 1809|21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
 1910|78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
 2011|16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
 2112|86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
 2213|19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
 2314|04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
 2415|88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
 2516|04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
 2617|20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
 2718|20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
 2819|01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48
 29
 30The product of these numbers is 26 × 63 × 78 × 14 = 1788696.
 31
 32  What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20×20 grid?
 33
 34## design
 35
 36### build a grid (brute force)
 37
 38 [
 39  [08, 02, 22, 97, 38, 15, 00, 40, 00, 75, 04, 05, 07, 78, 52, 12, 50, 77, 91, 08],
 40  [49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 04, 56, 62, 00],
 41 ]
 42
 43### build a graph
 44#
 45  do a traversal through the graph
 46  compute the largest product from each node
 47
 48   08 - 49
 49  /  \
 50 02  49
 51=end
 52
 53  class Heading
 54    attr_reader :grid
 55
 56    def initialize(grid)
 57      @grid = grid
 58    end
 59
 60    def max_product(digits, cell)
 61      next_cell = next_cell(cell)
 62      return cell.value if next_cell.nil?
 63      cell.value * max_product(digits - 1, next_cell)
 64    end
 65  end
 66
 67  class North < Heading
 68    def initialize(*args); super; end
 69    def next_cell(cell)
 70      grid.cell_at(x: cell.x - 1, y: cell.y)
 71    end
 72  end
 73  class South < Heading
 74    def initialize(*args); super; end
 75    def next_cell(cell)
 76      grid.cell_at(x: cell.x + 1, y: cell.y)
 77    end
 78  end
 79  class East < Heading
 80    def initialize(*args); super; end
 81    def next_cell(cell)
 82      grid.cell_at(x: cell.x, y: cell.y + 1)
 83    end
 84  end
 85  class West < Heading
 86    def initialize(*args); super; end
 87    def next_cell(cell)
 88      grid.cell_at(x: cell.x, y: cell.y - 1)
 89    end
 90  end
 91
 92  class BinaryHeading < Heading
 93    def initialize(grid, left, right)
 94      @left, @right = left, right
 95      super(grid)
 96    end
 97
 98    def next_cell(cell)
 99      left_cell = @left.next_cell(cell)
100      return nil if left_cell.nil?
101      @right.next_cell(left_cell)
102    end
103  end
104
105  class Cell
106    attr_reader :x, :y, :value
107
108    def initialize(grid: , x:, y:, value:)
109      @grid, @x, @y, @value = grid, x, y, value
110      @north, @east, @south, @west = North.new(grid), East.new(grid), South.new(grid), West.new(grid)
111    end
112
113    def max_product(digits)
114      headings.map { |x| x.max_product(digits, self) }.max
115    end
116
117    private
118
119    def headings
120      @headings ||= [
121        @north, BinaryHeading.new(@grid, @north, @east), 
122        @east, BinaryHeading.new(@grid, @south, @east), 
123        @south, BinaryHeading.new(@grid, @south, @west), 
124        @west, BinaryHeading.new(@grid, @north, @west), 
125      ]
126    end
127  end
128
129  class Grid
130    attr_reader :cells
131
132    def initialize(cells = [])
133      @cells = cells
134    end
135
136    def add_cell(x:, y:, value:)
137      cells.push(Cell.new(grid: self, x: x, y: y, value: value))
138    end
139
140    def row_count
141      cells.map { |x| x.x }.uniq!.count
142    end
143
144    def [](row)
145      cells.find_all { |cell| cell.x == row }
146    end
147
148    def max_product(digits)
149      cells.map { |cell| cell.max_product(digits) }.max
150    end
151
152    def cell_at(x:, y:)
153      cells.find { |cell| cell.x == x && cell.y == y }
154    end
155
156    def self.build_from(items)
157      grid = Grid.new
158      items.each_with_index do |row, row_index|
159        row.each_with_index do |cell, cell_index|
160          grid.add_cell(x: row_index, y: cell_index, value: cell)
161        end
162      end
163      grid
164    end
165  end
166
167  class GreatestProduct
168    attr_reader :grid
169
170    def initialize(raw_grid)
171      @raw_grid = raw_grid
172      @grid = build_grid_from(raw_grid)
173    end
174
175    def greatest_product(digits:)
176      grid.max_product(digits)
177    end
178
179    private
180
181    def build_grid_from(raw_grid)
182      items = [].tap do |grid|
183        StringIO.new(raw_grid).each_line do |line|
184          grid.push(line.split.map(&:to_i))
185        end
186      end
187      Grid.build_from(items)
188    end
189  end
190
191  subject { GreatestProduct.new(raw_grid) }
192
193  let(:raw_grid) do
194    <<-GRID
19508 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
19649 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
19781 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
19852 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
19922 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
20024 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
20132 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
20267 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
20324 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
20421 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
20578 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
20616 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
20786 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
20819 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
20904 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
21088 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
21104 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
21220 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
21320 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
21401 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48
215    GRID
216  end
217
218  it "returns the greatest product" do
219    expect(subject.greatest_product(digits: 3)).to eql([])
220  end
221
222  it 'builds a grid from the raw grid' do
223    raw_grid = <<-GRID
22408 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
22549 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
226    GRID
227    subject = GreatestProduct.new(raw_grid)
228    expect(subject.grid.row_count).to eql(2)
229    expect(subject.grid[0].map { |x| x.value }).to eql([8, 02, 22, 97, 38, 15, 0, 40, 0, 75, 04, 05, 07, 78, 52, 12, 50, 77, 91, 8])
230    expect(subject.grid[1].map { |x| x.value }).to eql([49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 04, 56, 62, 00])
231  end
232end