#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL encoded_data AS STRING
    LOCAL decoded_out AS STRING
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL run_count AS LONG
    LOCAL current_char AS STRING
    LOCAL temp_char AS STRING
    LOCAL state AS LONG
    LOCAL count_val AS LONG
    LOCAL count_str AS STRING
    LOCAL temp_str AS STRING

    PRINT "Run-Length Encoding Decoder"
    PRINT "=========================="
    PRINT ""

    ' RLE encoded format: each run is count followed by character
    ' e.g., "5A3B2C" = AAAAABBBCC
    encoded_data = "10A5B3C2D1E"
    decoded_out = ""

    PRINT "Encoded: " + encoded_data
    PRINT ""

    i = 1
    state = 0
    count_str = ""
    count_val = 0

    WHILE i <= LEN(encoded_data)
        temp_char = MID$(encoded_data, i, 1)

        IF state = 0 THEN
            count_str = ""
            count_val = 0
            state = 1
        END IF

        IF state = 1 THEN
            IF ASC(temp_char) >= 48 AND ASC(temp_char) <= 57 THEN
                count_str = count_str + temp_char
            ELSE
                count_val = 0
                j = 1
                WHILE j <= LEN(count_str)
                    count_val = count_val * 10 + (ASC(MID$(count_str, j, 1)) - 48)
                    j = j + 1
                WEND

                current_char = temp_char
                run_count = 0
                WHILE run_count < count_val
                    decoded_out = decoded_out + current_char
                    run_count = run_count + 1
                WEND

                state = 1
                count_str = ""
                count_val = 0
            END IF
        END IF

        i = i + 1
    WEND

    PRINT "Decoded: " + decoded_out
    PRINT "Length: " + STR$(LEN(decoded_out))
    PRINT ""

    ' Encode then decode
    encoded_data = "12W3X9Y4Z"
    decoded_out = ""

    PRINT "Test 2: " + encoded_data

    i = 1
    count_str = ""
    count_val = 0
    state = 1

    WHILE i <= LEN(encoded_data)
        temp_char = MID$(encoded_data, i, 1)

        IF ASC(temp_char) >= 48 AND ASC(temp_char) <= 57 THEN
            count_str = count_str + temp_char
        ELSE
            count_val = 0
            j = 1
            WHILE j <= LEN(count_str)
                count_val = count_val * 10 + (ASC(MID$(count_str, j, 1)) - 48)
                j = j + 1
            WEND

            current_char = temp_char
            run_count = 0
            WHILE run_count < count_val
                decoded_out = decoded_out + current_char
                run_count = run_count + 1
            WEND

            count_str = ""
            count_val = 0
        END IF

        i = i + 1
    WEND

    PRINT "Decoded: " + decoded_out
    PRINT ""

    ' Complex RLE: 7A1B6C
    encoded_data = "7A1B6C"
    decoded_out = ""

    i = 1
    count_str = ""
    WHILE i <= LEN(encoded_data)
        temp_char = MID$(encoded_data, i, 1)
        IF ASC(temp_char) >= 48 AND ASC(temp_char) <= 57 THEN
            count_str = count_str + temp_char
        ELSE
            count_val = 0
            j = 1
            WHILE j <= LEN(count_str)
                count_val = count_val * 10 + (ASC(MID$(count_str, j, 1)) - 48)
                j = j + 1
            WEND
            current_char = temp_char
            run_count = 0
            WHILE run_count < count_val
                decoded_out = decoded_out + current_char
                run_count = run_count + 1
            WEND
            count_str = ""
        END IF
        i = i + 1
    WEND

    PRINT "7A1B6C decoded: " + decoded_out
    PRINT ""

    ' Generate RLE from plain text
    temp_str = "AAABBBCCCDDD"
    PRINT "Encoding '" + temp_str + "':"
    i = 1
    WHILE i <= LEN(temp_str)
        current_char = MID$(temp_str, i, 1)
        count_val = 1
        j = i + 1
        WHILE j <= LEN(temp_str) AND MID$(temp_str, j, 1) = current_char
            count_val = count_val + 1
            j = j + 1
        WEND
        PRINT "  " + CHR$(48 + count_val) + current_char
        i = j
    WEND

    PRINT ""
    PRINT "RLE decoder demo complete."

    FUNCTION = 0
END FUNCTION
