mirror of
https://github.com/onyx-and-iris/aoc2025.git
synced 2025-12-08 11:47:47 +00:00
make {Grid}.data private
define delegated methods
This commit is contained in:
parent
2add30a6c1
commit
f9e99d1d2c
@ -1,10 +1,14 @@
|
|||||||
|
require "forwardable"
|
||||||
|
|
||||||
require_relative "cell"
|
require_relative "cell"
|
||||||
|
|
||||||
class Grid
|
class Grid
|
||||||
attr_reader :rows, :columns
|
attr_reader :columns
|
||||||
|
extend Forwardable
|
||||||
|
def_delegators :@data, :size, :[]
|
||||||
|
|
||||||
def initialize(input)
|
def initialize(input)
|
||||||
@rows = []
|
@data = []
|
||||||
(0...input.size).each do |row_index|
|
(0...input.size).each do |row_index|
|
||||||
row = []
|
row = []
|
||||||
(0...input[row_index].size).each do |column_index|
|
(0...input[row_index].size).each do |column_index|
|
||||||
@ -12,27 +16,26 @@ class Grid
|
|||||||
cell.value = input[row_index][column_index]
|
cell.value = input[row_index][column_index]
|
||||||
row << cell
|
row << cell
|
||||||
end
|
end
|
||||||
@rows << row
|
@data << row
|
||||||
end
|
end
|
||||||
@columns = @rows.transpose
|
|
||||||
|
|
||||||
# Link neighbours
|
# Link neighbours
|
||||||
(0...@rows.size).each do |row_index|
|
(0...@data.size).each do |row_index|
|
||||||
(0...@columns.size).each do |column_index|
|
(0...@data[row_index].size).each do |column_index|
|
||||||
cell = @rows[row_index][column_index]
|
cell = @data[row_index][column_index]
|
||||||
cell.n = @rows[row_index - 1][column_index] if row_index - 1 >= 0
|
cell.n = @data[row_index - 1][column_index] if row_index - 1 >= 0
|
||||||
cell.ne = @rows[row_index - 1][column_index + 1] if row_index - 1 >= 0 && column_index + 1 < @columns.size
|
cell.ne = @data[row_index - 1][column_index + 1] if row_index - 1 >= 0 && column_index + 1 < @data[row_index - 1].size
|
||||||
cell.e = @rows[row_index][column_index + 1] if column_index + 1 < @columns.size
|
cell.e = @data[row_index][column_index + 1] if column_index + 1 < @data[row_index].size
|
||||||
cell.se = @rows[row_index + 1][column_index + 1] if row_index + 1 < @rows.size && column_index + 1 < @columns.size
|
cell.se = @data[row_index + 1][column_index + 1] if row_index + 1 < @data.size && column_index + 1 < @data[row_index + 1].size
|
||||||
cell.s = @rows[row_index + 1][column_index] if row_index + 1 < @rows.size
|
cell.s = @data[row_index + 1][column_index] if row_index + 1 < @data.size
|
||||||
cell.sw = @rows[row_index + 1][column_index - 1] if row_index + 1 < @rows.size && column_index - 1 >= 0
|
cell.sw = @data[row_index + 1][column_index - 1] if row_index + 1 < @data.size && column_index - 1 >= 0
|
||||||
cell.w = @rows[row_index][column_index - 1] if column_index - 1 >= 0
|
cell.w = @data[row_index][column_index - 1] if column_index - 1 >= 0
|
||||||
cell.nw = @rows[row_index - 1][column_index - 1] if row_index - 1 >= 0 && column_index - 1 >= 0
|
cell.nw = @data[row_index - 1][column_index - 1] if row_index - 1 >= 0 && column_index - 1 >= 0
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def cell_at(row, column)
|
def cell_at(row, column)
|
||||||
@rows[row][column]
|
@data[row][column]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user