40 lines
857 B
Ruby
Executable File

#!/usr/bin/env ruby
# frozen_string_literal: true
require_relative "graph"
class Main
def run
input = $stdin.readlines(chomp: true)
graph = Graph.new(input)
counts = Hash.new(0)
counts[graph.start.x] = 1
process_graph(graph, counts)
puts counts.values.sum
end
def process_graph(graph, counts)
graph.each_with_index do |line, y|
line.each_with_index do |char, x|
next unless char == "^"
distribute_counts(x, y, counts)
end
end
end
# When we encounter a "^", we split the count to the west and east positions
# and reset the current position count to 0.
def distribute_counts(x, y, counts)
peek_west = Point.new(x - 1, y)
peek_east = Point.new(x + 1, y)
counts[peek_west.x] += counts[x]
counts[peek_east.x] += counts[x]
counts[x] = 0
end
end
Main.new.run