mirror of
https://github.com/onyx-and-iris/aoc2025.git
synced 2025-12-08 19:57:48 +00:00
delegate each_with_index
This commit is contained in:
parent
22afdfb126
commit
3db1c4c372
12
day_07/2.rb
12
day_07/2.rb
@ -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) == "^"
|
||||||
|
|||||||
@ -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|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user