Created
September 19, 2008 10:56
-
-
Save halorgium/11580 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
main = "jf*2r112r2" | |
f1 = "22r" | |
f2 = "ff*" | |
f1.gsub!(/2/, f2) | |
f2.gsub!(/1/, f1) | |
main.gsub!(/1/, f1) | |
main.gsub!(/2/, f2) | |
puts main | |
puts main == "jf*ff*rff*ff*rff*ff*rff*rff*" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Function | |
def initialize | |
@instructions = [] | |
end | |
def jump | |
@instructions << [:jump] | |
end | |
def forward | |
@instructions << [:forward] | |
end | |
def light | |
@instructions << [:light] | |
end | |
def call(fun) | |
@instructions << [:call, fun] | |
end | |
def left | |
@instructions << [:left] | |
end | |
def right | |
@instructions << [:right] | |
end | |
def execute(funs) | |
output = "" | |
@instructions.each do |(name,arg)| | |
case name | |
when :jump | |
output << '^' | |
when :forward | |
output << '+' | |
when :light | |
output << '*' | |
when :call | |
output << funs[arg].execute(funs) | |
when :left | |
output << '<' | |
when :right | |
output << '>' | |
else | |
raise "Unknown instruction: #{name.inspect}" | |
end | |
end | |
output | |
end | |
end | |
def fun(name, &block) | |
@funs ||= {} | |
f = Function.new | |
f.instance_eval(&block) | |
@funs[name] = f | |
end | |
def run | |
@funs[:main].execute(@funs) | |
end | |
fun :main do | |
jump | |
left | |
jump | |
call 2 | |
call 2 | |
call 2 | |
call 2 | |
end | |
fun 1 do | |
end | |
fun 2 do | |
light | |
jump | |
light | |
forward | |
light | |
jump | |
right | |
end | |
result = run | |
expected = "^<^*^*+*^>*^*+*^>*^*+*^>*^*+*" | |
puts " Result: " + result | |
puts "Expected: " + expected | |
print " " | |
result.size.times do |i| | |
print((result[i] == expected[i]) ? "." : " ") | |
end | |
puts | |
puts expected == result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment