From c2b1ece7f7de201a5c9523a77ecdbb47fbef2678 Mon Sep 17 00:00:00 2001 From: The-Tysonator Date: Mon, 24 Apr 2023 15:47:16 +0100 Subject: [PATCH] initial commit --- .gitignore | 1 + main.py | 138 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100644 .gitignore create mode 100755 main.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b7ff13a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_STORE \ No newline at end of file diff --git a/main.py b/main.py new file mode 100755 index 0000000..b2dc7d2 --- /dev/null +++ b/main.py @@ -0,0 +1,138 @@ +# Imports +import re +from ast import literal_eval + + +# LevelData Class +class LevelData(): + + + # Validate Level Data + @staticmethod + def validate(level_data): + # Variables + level_data = level_data + number_of_items = 0 + number_of_items_buffer = 0 + # Regular Expression + numbers = re.compile(r"\d") + # Check and Return Data + try: + data = literal_eval(level_data) + for list_number, list_value in enumerate(data): + if list_number == 0: + for number, number_value in enumerate(list_value): + if number_value < 0 or number_value > 9: + return False + number_of_items += 1 + else: + for number, number_value in enumerate(list_value): + if number_value < 0 or number_value > 9: + return False + number_of_items_buffer += 1 + if number_of_items_buffer != number_of_items: + return False + number_of_items_buffer = 0 + return True + except: + return False + + + # Compress Level Data + @staticmethod + def compress(level_data): + # Variables + level_data = level_data + cleared_level_data = "" + compressed_level_data = "" + number_of_matches = 0 + current_character_count = 0 + # Regular Expression + numbers = re.compile(r"\d") + # Add Prefix + data = literal_eval(level_data) + matches = numbers.finditer(str(data[0])) + for match in matches: + number_of_matches += 1 + compressed_level_data += f"{number_of_matches}%" + # Clear Data + for character_number, character_value in enumerate((level_data)): + try: + character_value = int(character_value) + cleared_level_data += str(character_value) + except: + pass + # Compress Data + for character_number, character_value in enumerate(cleared_level_data): + if character_number <= len(cleared_level_data) - 2: + if cleared_level_data[character_number] == cleared_level_data [character_number + 1]: + current_character_count += 1 + else: + current_character_count += 1 + if current_character_count > 5: + compressed_level_data += f"!{character_value}_{current_character_count}!" + current_character_count = 0 + else: + for character in range(current_character_count): + compressed_level_data += f"{character_value}" + current_character_count = 0 + else: + if current_character_count > 4: + compressed_level_data += f"!{character_value}_{current_character_count}!" + current_character_count = 0 + else: + for character in range(current_character_count): + compressed_level_data += f"{character_value}" + current_character_count = 0 + compressed_level_data += f"{character_value}" + # Return Data + return compressed_level_data + + + # Decompress Level Data + @staticmethod + def decompress(level_data): + # Variables + level_data = level_data + decompressed_level_data = "" + final_level_data = "" + compressed = False + compressed_data_buffer = "" + final_data_buffer = "" + final_list_counter = 0 + # Find Level Length + data = level_data.split("%") + # Decompress Data + for character_number, character_value in enumerate(data[1]): + if character_value == "!": + if compressed == False: + compressed = True + elif compressed == True: + compressed = False + compressed_data_buffer += f"{character_value}" + compressed_data_buffer = compressed_data_buffer.replace("!", "") + compressed_data_buffer = compressed_data_buffer.split("_") + for character in range(int(compressed_data_buffer[1])): + decompressed_level_data += f"{compressed_data_buffer[0]}" + compressed_data_buffer = "" + if compressed: + compressed_data_buffer += f"{character_value}" + elif character_value != "!": + decompressed_level_data += f"{character_value}" + # Create Final Level Data + final_level_data += "[" + for character_number, character_value in enumerate(decompressed_level_data): + if len(final_data_buffer) != int(data[0]) - 1: + final_data_buffer += f"{character_value}" + else: + final_data_buffer += f"{character_value}" + final_level_data += "[" + for character_number_two, character_value_two in enumerate(final_data_buffer): + final_level_data += f"{character_value_two}," + final_level_data = final_level_data.rstrip(final_level_data[-1]) + final_level_data += "]," + final_data_buffer = "" + final_level_data = final_level_data.rstrip(final_level_data[-1]) + final_level_data += "]" + # Return Data + return final_level_data \ No newline at end of file