Re: The Turing Machine (#162)
by ThoML other posts by this author
May 11 2008 10:34AM messages near this date
Re: The Turing Machine (#162)
|
Re: The Turing Machine (#162)
> It looks like it's been 48 hours
Already. Ok, so here is mine.
#!/usr/bin/env ruby
def tm(rules, q, input)
directions = {'L' => -1, 'R' => 1}
tape = input ? input.split(//) : []
p = 0
loop do
q, c, d = rules[[q, tape[p] || '_']]
return tape.join unless q
tape[p] = c == '_' ? nil : c
p += directions[d] || raise('Unknown direction: %s' % d)
if p == -1
tape.unshift(nil)
p = 0
end
end
end
def read_rules(file)
rules = {}
q = nil
File.readlines(file).each do |l|
a = l.scan(/#|\S+/)
next if a[0] == '#' or a.empty?
q ||= a[0]
rules[a[0,2]] = a[2,5]
end
return [rules, q]
end
if __FILE__ == $0
file, input = ARGV
puts tm(*read_rules(file), input)
end
Thread:
Matthew Moss
Adam Shelly
James Koppel
Alpha Chen
Matthew Moss
Alpha Chen
Alpha Chen
Matthew Moss
Robert Dober
Alpha Chen
Matthew Moss
Chiyuan Zhang
Juanger
Mike Stok
jgabrielygalan
Marcelo
Chiyuan Zhang
James Gray
Juanger
Matthew Moss
ThoML
Chris Shea
Chris Carter
Andrew Thompson
Sandro Paganotti
Chris Shea
ThoML
Chris Shea
Chris Shea
Glen F. Pankow
Adam Shelly
Matthew Moss
Chris Carter
Adam Shelly
|