#!/usr/bin/env ruby # frozen_string_literal: true require_relative "graph" class Main def run lines = $stdin.readlines(chomp: true) graph = Graph.new(lines) counts = Hash.new(0) counts[graph.start.x] = 1 process_graph(lines, graph, counts) puts counts.values.sum end def process_graph(lines, graph, counts) lines.each_with_index do |line, y| line.chars.each_with_index do |char, x| current = Point.new(x, y) next unless graph.at(current) == "^" 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