mirror of
https://github.com/onyx-and-iris/aoc2025.git
synced 2025-12-08 11:47:47 +00:00
add day_04
This commit is contained in:
parent
2a3c0d36a0
commit
18c73c1bb7
33
day_04/1.rb
Executable file
33
day_04/1.rb
Executable file
@ -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
|
||||
40
day_04/2.rb
Executable file
40
day_04/2.rb
Executable file
@ -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
|
||||
22
day_04/cell.rb
Normal file
22
day_04/cell.rb
Normal file
@ -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
|
||||
42
day_04/grid.rb
Normal file
42
day_04/grid.rb
Normal file
@ -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
|
||||
Loading…
x
Reference in New Issue
Block a user