Laby_ruby

Just found Laby. You program a robot ant through different mazes.
You can use:

  • c
  • cpp
  • go
  • lua
  • ocaml
  • pascal
  • perl
  • python
  • ruby
  • scheme
  • (or define any language you want)

This is the solution to problem 1c.

  def ant
    ffw
    hold { l2 }
    go %(2l ffw ex)
  end

.. using some helper-functions.

require "./robot"

class Ant < Robot

  def void? = ( look() == Void )
  def web? = ( look() == Web )
  def stone? = ( look() == Rock )
  def door? = ( look() == Exit )
  def wall? = ( look() == Wall )

  def l = left
  def r = right
  def l2 = 2.times { left }
  def r2 = l2

  def fw = forward
  def ffw = ( forward while void? )

  def ex = escape

  def hold(&bl)
    take
    yield
    drop
  end
  
  def go(inst, rev: false, inv: false)
    cmds = inst.split
    cmds = cmds.reverse if rev
    cmds.map! { it.tr('rl', 'lr') } if inv
    cmds.each {|cmd|
      case cmd
      when /(\d+)(\w+)/
        $1.to_i.times { eval $2 }
       else eval cmd  
      end
    }
  end

  # problem 1c
  def ant
    ffw
    hold { l2 }
    go %(2l ffw ex)
  end
end

Ant.new