From f9e99d1d2c350098c5d5b8d751bd286ae9d9e93a Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Fri, 5 Dec 2025 02:01:22 +0000 Subject: [PATCH] make {Grid}.data private define delegated methods --- day_04/grid.rb | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/day_04/grid.rb b/day_04/grid.rb index 2c7e228..78e7a46 100644 --- a/day_04/grid.rb +++ b/day_04/grid.rb @@ -1,10 +1,14 @@ +require "forwardable" + require_relative "cell" class Grid - attr_reader :rows, :columns + attr_reader :columns + extend Forwardable + def_delegators :@data, :size, :[] def initialize(input) - @rows = [] + @data = [] (0...input.size).each do |row_index| row = [] (0...input[row_index].size).each do |column_index| @@ -12,27 +16,26 @@ class Grid cell.value = input[row_index][column_index] row << cell end - @rows << row + @data << row end - @columns = @rows.transpose # Link neighbours - (0...@rows.size).each do |row_index| - (0...@columns.size).each do |column_index| - cell = @rows[row_index][column_index] - cell.n = @rows[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.e = @rows[row_index][column_index + 1] if column_index + 1 < @columns.size - cell.se = @rows[row_index + 1][column_index + 1] if row_index + 1 < @rows.size && column_index + 1 < @columns.size - cell.s = @rows[row_index + 1][column_index] if row_index + 1 < @rows.size - cell.sw = @rows[row_index + 1][column_index - 1] if row_index + 1 < @rows.size && column_index - 1 >= 0 - cell.w = @rows[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 + (0...@data.size).each do |row_index| + (0...@data[row_index].size).each do |column_index| + cell = @data[row_index][column_index] + cell.n = @data[row_index - 1][column_index] if row_index - 1 >= 0 + cell.ne = @data[row_index - 1][column_index + 1] if row_index - 1 >= 0 && column_index + 1 < @data[row_index - 1].size + cell.e = @data[row_index][column_index + 1] if column_index + 1 < @data[row_index].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 = @data[row_index + 1][column_index] if row_index + 1 < @data.size + cell.sw = @data[row_index + 1][column_index - 1] if row_index + 1 < @data.size && column_index - 1 >= 0 + cell.w = @data[row_index][column_index - 1] if 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 def cell_at(row, column) - @rows[row][column] + @data[row][column] end end