diff --git a/day_04/1.rb b/day_04/1.rb new file mode 100755 index 0000000..c679eae --- /dev/null +++ b/day_04/1.rb @@ -0,0 +1,33 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +require_relative "grid" + +class Main + def run + data = [] + $stdin.each_line do |line| + data << line.chomp + end + + @sum = 0 + + grid = Grid.new(data) + (0...grid.rows.size).each do |row_index| + (0...grid.columns.size).each do |column_index| + next unless grid.cell_at(row_index, column_index).value == "@" + + memo = grid.cell_at(row_index, column_index).neighbours.reject do |cell| + grid.cell_at(cell.row, cell.column).value != "@" + end + if memo.size < 4 + @sum += 1 + end + end + end + + puts @sum + end +end + +Main.new.run diff --git a/day_04/2.rb b/day_04/2.rb new file mode 100755 index 0000000..ec39356 --- /dev/null +++ b/day_04/2.rb @@ -0,0 +1,40 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +require_relative "grid" + +class Main + def run + data = [] + $stdin.each_line do |line| + data << line.chomp + end + + @sum = 0 + + removed_sum = 1 + grid = Grid.new(data) + until removed_sum == 0 + removed_sum = 0 + (0...grid.rows.size).each do |row_index| + (0...grid.columns.size).each do |column_index| + next unless grid.cell_at(row_index, column_index).value == "@" + + memo = grid.cell_at(row_index, column_index).neighbours.reject do |cell| + grid.cell_at(cell.row, cell.column).value != "@" + end + if memo.size < 4 + data[row_index][column_index] = "x" + removed_sum += 1 + end + end + end + @sum += removed_sum + grid = Grid.new(data) + end + + puts @sum + end +end + +Main.new.run diff --git a/day_04/cell.rb b/day_04/cell.rb new file mode 100644 index 0000000..fe8b286 --- /dev/null +++ b/day_04/cell.rb @@ -0,0 +1,22 @@ +class Cell + attr_reader :row, :column + attr_accessor :value + attr_accessor :n, :ne, :e, :se, :s, :sw, :w, :nw + + def initialize(row, column) + @row, @column = row, column + end + + def neighbours + list = [] + list << n if n + list << ne if ne + list << e if e + list << se if se + list << s if s + list << sw if sw + list << w if w + list << nw if nw + list + end +end diff --git a/day_04/grid.rb b/day_04/grid.rb new file mode 100644 index 0000000..ca388df --- /dev/null +++ b/day_04/grid.rb @@ -0,0 +1,42 @@ +require "forwardable" + +require_relative "cell" + +class Grid + attr_reader :rows, :columns + extend Forwardable + def_delegators :@rows, :[] + + def initialize(input) + @rows = [] + (0...input.size).each do |row_index| + row = [] + (0...input[row_index].size).each do |column_index| + cell = Cell.new(row_index, column_index) + cell.value = input[row_index][column_index] + row << cell + end + @rows << 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 + end + end + end + + def cell_at(row, column) + @rows[row][column] + end +end