from sets import * # written by Ray Jones program = ['FM ETY O','IHIGLM DN','SGLHRAE T','SITAWK AE',' YUOREO R','LGRANFAHH','NY LAFSRW','FHMAETR U','GIT ET LW'] def op1(dx,dy): return (dx+1, dy) def op2(dx,dy): return (dx-1,dy) def op3(dx,dy): return (dx, dy+1) def op4(dx,dy): return (dx, dy-1) def op5(dx,dy): return (dx+1,dy+1) def op6(dx,dy): return (dx+1,dy-1) def op7(dx,dy): return (dx-1,dy+1) def op8(dx,dy): return (dx-1,dy-1) def op9(dx,dy): return (-dx,dy) def op10(dx,dy): return (dx,-dy) def op11(dx,dy): return (dy, dx) def op12(dx,dy): return (-dy, dx) def op13(dx,dy): return (dy,-dx) def op14(dx,dy): return (-dy,-dx) def op15(dx,dy): return (dx+1,-dy) def op16(dx,dy): return (dx-1,-dy) def op17(dx,dy): return (-dx,dy+1) def op18(dx,dy): return (-dx,dy-1) def op19(dx,dy): return 'STOP' ops = [op1,op2,op3,op4,op5,op6,op7,op8,op9,op10,op11,op12,op13,op14,op15,op16,op17,op18,op19] opmap = {} max_steps = 100 force = 'F' def execute(x, y, dx, dy, stepnum, output): letter = program[y % 9][x % 9] if (stepnum <= len(force)): if not (letter == force[stepnum-1]): return if (stepnum == 2): if not letter in 'AEIOUYHRL': return if letter in ' ': if not check_words(output.split(' ').pop()): return else: if not check_prefixes((output + letter).split(' ').pop()): return if (opmap.has_key(letter)): newdxdy = (opmap[letter])(dx, dy) if newdxdy == 'STOP': if len(opmap) == 2 * len(ops): print "%s STOP"%(output + letter) return else: try_each(x, y, dx, dy, stepnum, output, letter) return if (stepnum == max_steps): print "%s"%(output+letter) return (dx,dy) = newdxdy execute(x + dx, y + dy, dx, dy, stepnum+1, output + letter) def try_each(x, y, dx, dy, stepnum, output, letter): for op in ops: if not opmap.has_key(op): opmap[letter] = op opmap[op] = letter execute(x, y, dx, dy, stepnum, output) del opmap[letter] del opmap[op] words = Set() prefixes = Set() f=open('/home/thouis/FILES/wlist.txt','r') for word in f.readlines(): word = word.strip() words.add(word) for i in range(0,len(word)): prefixes.add(word[0:i]) prefixes.update(words) def check_words(a): w = a.lower() if (len(w) == 0): return False if (len(w) == 1): if w in 'AI': return True else: return False if (w in words): return True def check_prefixes(a): w = a.lower() if (w in prefixes): return True words2=['me','he','or','do','to','am','my','an','if','in','up','as','no','at','is','us','it','no','pi','we','of','be','so','by','go','on'] words.update(Set(words2)) prefixes.update(words) execute(0, 0, 0, 0, 1, "")