Sha256: 30bd0ef676d75cf189835854cdfd8cc8a122d1d57dbf820f9753e04da0acb3f7

Contents?: true

Size: 923 Bytes

Versions: 1

Compression:

Stored size: 923 Bytes

Contents

# frozen_string_literal: true

require 'forwardable'
module LabyrinthSolver
  # Takes a labyrinth and attempts to solve it saving the path taken
  class Solver
    attr_reader :path

    extend Forwardable

    OPPOSITE_DIRECTIONS = { up: :down, right: :left, down: :up, left: :right }.freeze

    def_delegators :@labyrinth, :position, :cheese?, :go, :close

    def initialize labyrinth
      raise ArgumentError unless labyrinth.instance_of? Labyrinth

      @labyrinth = labyrinth
      @path = []
    end

    def go_next
      next_dir = OPPOSITE_DIRECTIONS.find { |dir, opp| @labyrinth.open?(dir) && opp != @path.last }
      return dead_end unless next_dir

      go(next_dir.first)
      @path.push(next_dir.first)
    end

    def solve
      go_next until cheese?
    end

    private
    
    def dead_end
      to_close = path.pop
      go(OPPOSITE_DIRECTIONS[to_close])
      close(to_close)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
subparry_labyrinth_solver-1.0.4 lib/subparry_labyrinth_solver/solver.rb