#@name numberlink game
@inputs 
@outputs 
@persist 
@trigger 

Colors = table(
    "A" = vec(0, 0, 180),
    "B" = vec(175, 13, 102),
    "C" = vec(146, 248, 70),
    "D" = vec(255, 200, 47),
    "E" = vec(255, 118, 0),
    "F" = vec(185, 185, 185),
    "G" = vec(235, 235, 222),
    "H" = vec(100, 100, 100),
    "I" = vec(255, 255, 0),
    "J" = vec(55, 19, 112),
    "K" = vec(255, 255, 150),
    "L" = vec(202, 62, 94),
    "M" = vec(205, 145, 63),
    "N" = vec(12, 75, 100),
    "O" = vec(255, 0, 0),
    "P" = vec(175, 155, 50),
    "Q" = vec(0, 0, 74),
    "R" = vec(37, 70, 25),
    "S" = vec(121, 33, 135),
    "T" = vec(83, 140, 208),
    "U" = vec(0, 154, 37),
    "V" = vec(178, 220, 205),
    "W" = vec(255, 152, 213),
    "Y" = vec(175, 200, 74),
    "Z" = vec(63, 25, 12)
)

function string encode( String:string ) {
    local Encode = ""
    local String = String:upper()

    local Length = sqrt(String:length())

    for (Row = 0, Length - 1) {
        if (Row > 0) { Encode += "/" }
        
        local Empty = 0
        for (Col = 0, Length - 1) {
            local Char = String[Row*Length + Col + 1]

            if (Char == "X") {
                Empty++    
            } else {
                if (Empty > 0) { Encode += format("%d", Empty) }
                
                Empty = 0
                Encode += Char
            }
        }

        if (Empty > 0) { Encode += format("%d", Empty) } 
    }
 
    return Encode:trim()
}

function string decode( String:string ) {
    local Decode = ""

    foreach (K, Row:string = String:explode("/")) {
        for (I = 1, Row:length()) {
            local Sub = ""
            local Char = Row[I]

            if (Char:toNumber() > 0) {
                for (J = 1, Char:toNumber()) {
                    Sub += "X"
                }
            } else {
                Sub += Char
            }
            Decode += Sub
        } 
    }

    return Decode
}



S = "xxxxxxbxbyxxxxxgyxrgxxxxr"
S = "xxxxxxhxxxbxxbpyxxxxxxxxgxxoxxcxxrxxxxxxxxxxxxxxxxxxxxxpgxxyxoxxcxxxrxhxxxxxxxxxx"

E = encode(S)
D = decode(E)





print(E)

#local function removeDuplicates( str )
#	local newStr = ""
#	local lookup = {}
#	for i = 1, string.len( str ) do
#		local char = str[ i ]
#		
#		if lookup[ char ] then continue end
#		
#		newStr = newStr .. char
#		
#		lookup[ char ] = true
#	end
#	
#	return newStr
#end

function string randomizeColor( String:string ) {
    local Alphabet = "abcdefghijklmnopqrstuvwyz"
    local TempString = invert(String:replaceRE("[%l%d%p]", ""):explode("")):keys():concat()
    local NewString = String

    for (I = 1, TempString:length()) {
        local Random = ceil(random(1, Alphabet:length()))
        local NewChar = Alphabet[Random]
        
        NewString = NewString:replace(TempString[I], NewChar)
        Alphabet = Alphabet:replace(NewChar, "")
    }
    
    print(NewString:upper())
    print(String)
    #printTable(TempString)

}
