#!/usr/bin/env ruby # frozen_string_literal: true require_relative "graph" class Main def run input = $stdin.readlines(chomp: true) graph = Graph.new(input) total = dfs(graph.start, graph, 0, Set.new) puts total end def dfs(next_point, graph, total, visited) if next_point.y > graph.height || visited.include?(next_point) return total end if graph.at(next_point) == "^" step_west = Point.new(next_point.x - 1, next_point.y) step_east = Point.new(next_point.x + 1, next_point.y) if graph.at(step_west) == "." || graph.at(step_east) == "." total += 1 end # step west then dfs south total = dfs(step_west, graph, total, visited) # step east then dfs south total = dfs(step_east, graph, total, visited) return total elsif graph.at(next_point) == "." visited.add(next_point) end dfs(Point.new(next_point.x, next_point.y + 1), graph, total, visited) # south end end Main.new.run