Advertisement
Guest User

neu

a guest
Jul 3rd, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.11 KB | None | 0 0
  1. local tArgs = { ... }
  2.  
  3. if not tArgs[1] then
  4.   error("Incorrect Parameters. neu <program name>")
  5. end
  6.  
  7. local ref = "abcdefghijklmnopqrstuvwxyz\"()="
  8.  
  9. local input = {" ","\n",".",","}
  10. for n in ref:gmatch("[^.]") do
  11.   table.insert(input,n)
  12. end
  13.  
  14. local goalf = fs.open(tArgs[1],"r")
  15. local goal = goalf.readAll()
  16. goalf.close()
  17.  
  18. local tried = {}
  19. local thoughts = {}
  20.  
  21. function used(level, char)
  22.   if tried[level] then
  23.     return tried[level][char]
  24.   else
  25.     tried[level] = {}
  26.     return false
  27.   end
  28. end
  29.  
  30. function findVal(tab, val)
  31.   for k,v in pairs(tab) do
  32.     if v == val then
  33.       return true,k
  34.     end
  35.   end
  36.   return false
  37. end
  38.  
  39. function countTable(tab)
  40.   local n = 0
  41.   for _ in pairs(tab) do n = n + 1 end
  42.   return n
  43. end
  44.  
  45. function genChar()
  46.   local c
  47.   if not tried[#thoughts+1] then
  48.     tried[#thoughts+1] = {}
  49.   end
  50.   local suc,pkey = findVal(tried[#thoughts+1], true)
  51.   if suc then
  52.     c = pkey
  53.   else
  54.     if countTable(tried[#thoughts+1]) >= #input then
  55.       error("Unknown character")
  56.     end
  57.     --if goal:sub(#thoughts+1,#thoughts+1)=="|" then
  58.     --  printError("\n"..#tried[#thoughts+1])
  59.     --  printError(#input)
  60.     --  error("Wat... no catchy?")
  61.     --end
  62.     local stc = os.clock()
  63.     repeat
  64.       c = input[math.random(1,#input)]
  65.       if os.clock()-stc > 0.1 then
  66.         error("Caught foul exec time")
  67.       end
  68.     until used(#thoughts+1 , c) == nil --right here
  69.   end
  70.   return c
  71. end
  72.  
  73. function right()
  74.   local str = table.concat(thoughts)
  75.  
  76.   return str == goal
  77. end
  78.  
  79. write("> ")
  80. while not right() do
  81.   local pot = genChar()
  82.   local px,py = term.getCursorPos()
  83.   if pot=="\n" then
  84.     write(" ")
  85.   end
  86.   write(pot)
  87.   local pxx,pyy = term.getCursorPos()
  88.   if (table.concat(thoughts) or "")..pot == goal:sub(1,#thoughts+1) then
  89.     tried[#thoughts+1][pot] = true
  90.     table.insert(thoughts,pot)
  91.     sleep(0)
  92.   else
  93.     tried[#thoughts+1][pot] = false
  94.     term.setCursorPos(px,py)
  95.     sleep(0)
  96.   end
  97. end
  98. print("")
  99. loadstring(goal)() -- :3
  100.  
  101. local data = textutils.serialize(tried)
  102. local file = fs.open("data","w")
  103. file.write(data)
  104. file.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement