2025 ruby day 4
This commit is contained in:
parent
b2f13520ab
commit
3ace918ff2
7
ruby/2025/4/bin/problem
Normal file
7
ruby/2025/4/bin/problem
Normal file
@ -0,0 +1,7 @@
|
||||
#!/usr/bin/env ruby
|
||||
require "printing_department"
|
||||
|
||||
printing_department = PrintingDepartment.for STDIN.read.chomp
|
||||
puts "Part 1: #{printing_department.accessible_rolls.count}"
|
||||
printing_department.remove_all_accessible_rolls!
|
||||
puts "Part 2: #{printing_department.removed_rolls.count}"
|
||||
64
ruby/2025/4/lib/printing_department.rb
Normal file
64
ruby/2025/4/lib/printing_department.rb
Normal file
@ -0,0 +1,64 @@
|
||||
class PrintingDepartment
|
||||
attr_reader :rows, :removed_rolls
|
||||
def initialize(rows)
|
||||
@rows = rows
|
||||
@removed_rolls = []
|
||||
end
|
||||
|
||||
def self.for(string)
|
||||
new string.split("\n").map{ |row| row.split("") }
|
||||
end
|
||||
|
||||
def rolls
|
||||
rows.each_with_index.map do |row, row_index|
|
||||
row.each_with_index.map do |char, column_index|
|
||||
[row_index, column_index] if char == "@"
|
||||
end.compact
|
||||
end.flatten(1)
|
||||
end
|
||||
|
||||
def char_at(row, column)
|
||||
rows[row][column]
|
||||
end
|
||||
|
||||
def width
|
||||
rows[0].length
|
||||
end
|
||||
|
||||
def height
|
||||
rows.length
|
||||
end
|
||||
|
||||
def neighbor_indices(row, column)
|
||||
[0, row - 1].max.upto([width - 1, row + 1].min).map do |r|
|
||||
[0, column - 1].max.upto([height - 1, column + 1].min).map do |c|
|
||||
[r, c]
|
||||
end
|
||||
end.flatten(1).reject{ |r, c| r == row && c == column }
|
||||
end
|
||||
|
||||
def neighbors(row, column)
|
||||
neighbor_indices(row, column).map { |nr, nc| char_at nr, nc }
|
||||
end
|
||||
|
||||
def adjacent_roll_count(row, column)
|
||||
neighbors(row, column).select { |char| char == "@" }.count
|
||||
end
|
||||
|
||||
def accessible_rolls
|
||||
rolls.select{ |r, c| adjacent_roll_count(r, c) < 4 }
|
||||
end
|
||||
|
||||
def remove_roll!(row, col)
|
||||
removed_rolls << [row, col]
|
||||
rows[row][col] = "."
|
||||
end
|
||||
|
||||
def remove_accessible_rolls!
|
||||
accessible_rolls.each { |r, c| remove_roll! r, c }
|
||||
end
|
||||
|
||||
def remove_all_accessible_rolls!
|
||||
remove_accessible_rolls! until accessible_rolls.empty?
|
||||
end
|
||||
end
|
||||
40
ruby/2025/4/test/test_printing_department.rb
Normal file
40
ruby/2025/4/test/test_printing_department.rb
Normal file
@ -0,0 +1,40 @@
|
||||
require "minitest/autorun"
|
||||
require "printing_department"
|
||||
|
||||
class TestPrintingDepartment < Minitest::Test
|
||||
def test_accessible_rolls
|
||||
map = <<END
|
||||
..@@.@@@@.
|
||||
@@@.@.@.@@
|
||||
@@@@@.@.@@
|
||||
@.@@@@..@.
|
||||
@@.@@@@.@@
|
||||
.@@@@@@@.@
|
||||
.@.@.@.@@@
|
||||
@.@@@.@@@@
|
||||
.@@@@@@@@.
|
||||
@.@.@@@.@.
|
||||
END
|
||||
|
||||
accessible_rolls_map = <<END
|
||||
..xx.xx@x.
|
||||
x@@.@.@.@@
|
||||
@@@@@.x.@@
|
||||
@.@@@@..@.
|
||||
x@.@@@@.@x
|
||||
.@@@@@@@.@
|
||||
.@.@.@.@@@
|
||||
x.@@@.@@@@
|
||||
.@@@@@@@@.
|
||||
x.x.@@@.x.
|
||||
END
|
||||
|
||||
printing_department = PrintingDepartment.for map
|
||||
assert_equal(13, printing_department.accessible_rolls.count)
|
||||
printing_department.remove_accessible_rolls!
|
||||
assert_equal(12, printing_department.accessible_rolls.count)
|
||||
printing_department.remove_all_accessible_rolls!
|
||||
assert_equal(0, printing_department.accessible_rolls.count)
|
||||
assert_equal(43, printing_department.removed_rolls.count)
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue
Block a user