delegate each_with_index

This commit is contained in:
onyx-and-iris 2025-12-08 03:32:55 +00:00
parent 22afdfb126
commit 3db1c4c372
2 changed files with 7 additions and 7 deletions

View File

@ -5,18 +5,18 @@ require_relative "graph"
class Main class Main
def run def run
lines = $stdin.readlines(chomp: true) input = $stdin.readlines(chomp: true)
graph = Graph.new(lines) graph = Graph.new(input)
counts = Hash.new(0) counts = Hash.new(0)
counts[graph.start.x] = 1 counts[graph.start.x] = 1
process_graph(lines, graph, counts) process_graph(graph, counts)
puts counts.values.sum puts counts.values.sum
end end
def process_graph(lines, graph, counts) def process_graph(graph, counts)
lines.each_with_index do |line, y| graph.each_with_index do |line, y|
line.chars.each_with_index do |char, x| line.each_with_index do |char, x|
current = Point.new(x, y) current = Point.new(x, y)
next unless graph.at(current) == "^" next unless graph.at(current) == "^"

View File

@ -5,7 +5,7 @@ require_relative "point"
class Graph class Graph
attr_reader :start, :height, :width attr_reader :start, :height, :width
extend Forwardable extend Forwardable
def_delegators :@data, :[] def_delegators :@data, :[], :each_with_index
def initialize(input) def initialize(input)
@data = input.each_with_index.map do |line, y| @data = input.each_with_index.map do |line, y|