------art_13184_29610905.1210524058341
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

On Fri, May 9, 2008 at 9:48 AM, Matthew Moss <matthew.moss / gmail.com> wrote:

> This week's task is to build a Turing Machine, so we can play around
> with the architecture.

My solution is attached.  I'm not satisfied with it, I have the
feeling that it could be improved in several places, but it does work.

I TDDed this, but when I got to writing the "Instructions" class, I
couldn't (quickly) come up with a reasonable way of TTDing it.  It
probably means that I put too much stuff in each method, which is
likely where my insatisfaction comes from.

Marcelo

------art_13184_29610905.1210524058341
Content-Type: application/x-ruby; name=turing.rb
Content-Transfer-Encoding: base64
X-Attachment-Id: f_fg3ukpnz0
Content-Disposition: attachment; filename=turing.rb

IyEvdXNyL2Jpbi9lbnYgcnVieQoKY2xhc3MgVGFwZQogICAgZGVmIGluaXRpYWxpemUoc3RyPW5p
bCkKICAgICAgICBAdGFwZSA9IChzdHIubmlsPyA/ICIiIDogc3RyLmR1cCkKICAgICAgICBAcG9z
aXRpb24gPSAwCiAgICBlbmQKCiAgICBkZWYgbGVmdAogICAgICAgIGlmIEBwb3NpdGlvbiA9PSAw
CiAgICAgICAgICAgIEB0YXBlLmluc2VydChAcG9zaXRpb24sICJfIikKICAgICAgICBlbHNlCiAg
ICAgICAgICAgIEBwb3NpdGlvbiAtPSAxCiAgICAgICAgZW5kCiAgICBlbmQKCiAgICBkZWYgcmln
aHQKICAgICAgICBlbnN1cmVfY3VycmVudF9wb3NpdGlvbl9leGlzdHMKICAgICAgICBAcG9zaXRp
b24gKz0gMQogICAgZW5kCgogICAgZGVmIHJlYWQKICAgICAgICBlbnN1cmVfY3VycmVudF9wb3Np
dGlvbl9leGlzdHMKICAgICAgICBAdGFwZVtAcG9zaXRpb24sIDFdCiAgICBlbmQKCiAgICBkZWYg
d3JpdGUodmFsKQogICAgICAgIGVuc3VyZV9jdXJyZW50X3Bvc2l0aW9uX2V4aXN0cwogICAgICAg
IEB0YXBlW0Bwb3NpdGlvbl0gPSB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gcmUubWF0Y2go
bGluZSkgdGhlbgogICAgICAgICAgICAgICAgICAgIHN0ID0gZGF0YVsxXS50b19zeW0KICAgICAg
ICAgICAgICAgICAgICBjaCA9IGRhdGFbMl0KICAgICAgICAgICAgICAgICAgICBAaW5zdHJ1Y3Rp
b25zW3N0XVtjaF0gPSBUcmFuc2l0aW9uLm5ldygqZGF0YVszIC4uIDVdKQoKICAgICAgICAgICAg
ICAgICAgICBpZiBzZWxmLmluaXRpYWxfc3RhdGUubmlsPwogICAgICAgICAgICAgICAgICAgICAg
ICBzZWxmLmluaXRpYWxfc3RhdGUgPSBzdAogICAgICAgICAgICAgICAgICAgIGVuZAogICAgICAg
ICAgICAgICAgZW5kCiAgICAgICAgICAgIGVuZAogICAgICAgIGVuZAogICAgZW5kCgogICAgZGVm
IGdldF90cmFuc2l0aW9uKHN0YXRlLCBjaGFyKQogICAgICAgIGlmIEBpbnN0cnVjdGlvbnMuaGFz
X2tleT8oc3RhdGUpICYmIEBpbnN0cnVjdGlvbnNbc3RhdGVdLmhhc19rZXk/KGNoYXIpCiAgICAg
ICAgICAgIHJldHVybiBAaW5zdHJ1Y3Rpb25zW3N0YXRlXVtjaGFyXQogICAgICAgIGVsc2UKICAg
ICAgICAgICAgcmV0dXJuIG5pbAogICAgICAgIGVuZAogICAgZW5kCgogICAgZGVmIHJ1bih0YXBl
LCBzdGF0ZSA9IHNlbGYuaW5pdGlhbF9zdGF0ZSkKICAgICAgICByYWlzZSgiSW52YWxpZCB0YXBl
IikgdW5sZXNzCiAgICAgICAgICAgIHRhcGUucmVzcG9uZF90bz8oOmxlZnQpICAmJgogICAgICAg
ICAgICB0YXBlLnJlc3BvbmRfdG8/KDpyaWdodCkgJiYKICAgICAgICAgICAgdGFwZS5yZXNwb25k
X3RvPyg6cmVhZCkgICYmCiAgICAgICAgICAgIHRhcGUucmVzcG9uZF90bz8oOndyaXRlKQoKICAg
ICAgICB3aGlsZSB0ID0gZ2V0X3RyYW5zaXRpb24oc3RhdGUsIHRhcGUucmVhZCkKICAgICAgICAg
ICAgc3RhdGUgPSB0LmFwcGx5IGRvIHxjaCwgZGlyfAogICAgICAgICAgICAgICAgdGFwZS53cml0
ZShjaCkKICAgICAgICAgICAgICAgIGNhc2UgZGlyCiAgICAgICAgICAgICAgICAgICAgd2hlbiAn
TCcKICAgICAgICAgICAgICAgICAgICAgICAgdGFwZS5sZWZ0CiAgICAgICAgICAgICAgICAgICAg
d2hlbiAnUicKICAgICAgICAgICAgICAgICAgICAgICAgdGFwZS5yaWdodAogICAgICAgICAgICAg
ICAgICAgIGVsc2UKICAgICAgICAgICAgICAgICAgICAgICAgcmFpc2UgIkludmFsaWQgZGlyZWN0
aW9uIgogICAgICAgICAgICAgICAgZW5kCiAgICAgICAgICAgIGVuZAogICAgICAgIGVuZAogICAg
ZW5kCmVuZAoKaWYgX19GSUxFX18gPT0gJDAKICAgIGlucyA9IEluc3RydWN0aW9ucy5uZXcoQVJH
Vi5zaGlmdCkKICAgIHRhcGUgPSBUYXBlLm5ldyhBUkdWLnNoaWZ0KQoKICAgIGlucy5ydW4odGFw
ZSkKCiAgICBwdXRzIHRhcGUKZW5kCg------art_13184_29610905.1210524058341--