Class: Grid

Inherits:
Object
  • Object
show all
Defined in:
lib/number_grid.rb

Instance Method Summary collapse

Constructor Details

#initialize(height = 20, width = 20, filename = 'number_grid.txt') ⇒ Grid

Returns a new instance of Grid.



4
5
6
7
8
# File 'lib/number_grid.rb', line 4

def initialize(height = 20, width = 20, filename = 'number_grid.txt')
  @height = height
  @width = width
  @filename = filename
end

Instance Method Details

#getProduct(set) ⇒ Object



25
26
27
28
29
# File 'lib/number_grid.rb', line 25

def getProduct(set)
  product = 1
  set.each {|int| product *= int}
  product
end

#getVerticalLines(grid) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/number_grid.rb', line 52

def getVerticalLines(grid)
  x = 0
  verticalLines = []
  while x < grid[0].length do
    verticalList = []
    y = 0
    while y < @height do
      verticalList.push grid[y][x]
      y += 1
    end
    verticalLines.push verticalList
    x += 1
  end
  verticalLines
end

#greatestDiagonalProductLtoR(grid) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/number_grid.rb', line 77

def greatestDiagonalProductLtoR(grid)
  product_list = []
  y = 0
  while y < (@height - 3) do
    x = 0
    while x < (grid[0].length - 3) do
      set = [grid[y][x], grid[y + 1][x + 1], grid[y + 2][x + 2], grid[y + 3][x + 3]]
      product_list.push(getProduct(set))
      x += 1
    end
    y += 1
  end
  product_list.max
end

#greatestDiagonalProductRtoL(grid) ⇒ Object



100
101
102
103
# File 'lib/number_grid.rb', line 100

def greatestDiagonalProductRtoL(grid)
  reversed_grid = reverseGrid(grid)
  greatestDiagonalProductLtoR(reversed_grid)
end

#greatestHorizontalProduct(grid) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/number_grid.rb', line 42

def greatestHorizontalProduct(grid)
  i = 0
  product_list = []
  while i < @height do
    product_list.push greatestProductFromLine(grid[i])
    i += 1
  end
  product_list.max
end

#greatestProductFromLine(line) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/number_grid.rb', line 31

def greatestProductFromLine(line)
  product_list = []
  i = 0
  while i < line.length - 3 do
    set = line[i...(i + 4)]
    product_list.push getProduct(set)
    i += 1
  end
  product_list.max
end

#greatestVerticalProduct(grid) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/number_grid.rb', line 68

def greatestVerticalProduct(grid)
  product_list = []
  verticalLines = getVerticalLines(grid)
  verticalLines.each do |line|
    product_list.push greatestProductFromLine(line)
  end
  product_list.max
end

#numberStringToArray(str) ⇒ Object



10
11
12
13
# File 'lib/number_grid.rb', line 10

def numberStringToArray(str)
  list = Array.new(str.split(" "))
  list.map! {|n| n = n.to_i}
end

#readGridObject



15
16
17
18
19
20
21
22
23
# File 'lib/number_grid.rb', line 15

def readGrid()
  grid = []  
  File.open(@filename, 'r') do |f|
    f.each_line do |line|
      grid.push numberStringToArray(line)
    end
  end
  grid
end

#reverseGrid(grid) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/number_grid.rb', line 92

def reverseGrid(grid)
  reversed_grid = []
  grid.each do |line|
    reversed_grid.push(line.reverse)
  end
  reversed_grid
end

#solveObject



105
106
107
108
109
110
111
112
# File 'lib/number_grid.rb', line 105

def solve()
  grid = readGrid()
  ans = [
    greatestHorizontalProduct(grid), 
    greatestVerticalProduct(grid), 
    greatestDiagonalProductLtoR(grid), 
    greatestDiagonalProductRtoL(grid)].max
end