Skip to content

Instantly share code, notes, and snippets.

@skwerlman
Created August 27, 2015 14:38
Show Gist options
  • Save skwerlman/59a8522e13cae4516a12 to your computer and use it in GitHub Desktop.
Save skwerlman/59a8522e13cae4516a12 to your computer and use it in GitHub Desktop.
An implementation of xkcd 1513's 3rd insult
--[[
For best/most-reasonably-timed results, use with luajit
License: MIT
NOTE: this code has never finished running
--]]
local sourceString = 'This is a string. We will randomize a single character from it. After we do that, we will see if it runs as valid Lua code. If not, we will try again.'
local legalChars = {32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49, -- this is a list of chars we can expect to see in vaild lua
50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,
75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,
100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,
119,120,121,122,123,124,125,126}
local possibleChars = 95
local workingString = sourceString
local cycles = 0
print('0:\t' .. workingString)
local validLua = false
while not validLua do
cycles = cycles + 1
local charToBeChanged = math.random( #workingString )
local charToReplaceWith = legalChars[ math.random( possibleChars ) ]
local firstHalf = workingString:sub( 1, charToBeChanged - 1 )
local secondHalf = workingString:sub( charToBeChanged + 1, #workingString )
workingString = firstHalf .. string.char( charToReplaceWith ) .. secondHalf
if cycles % 5000 == 0 then -- only output once every 5000 iterations to maximize efficiency
print(cycles .. ':\t' .. workingString)
end
local out, err = pcall(workingString) -- (try to) run the code
validLua = out and true or false -- normalize out to true/false
end
print(sourceString)
print(workingString)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment