From 110c6303b08d74b463fc3c7f51ea50ac45f4909f Mon Sep 17 00:00:00 2001 From: Kees van Kempen Date: Wed, 8 Jan 2025 17:31:28 +0100 Subject: [PATCH] 2024(9): WIP part2 --- 2024/09/part2.py | 41 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/2024/09/part2.py b/2024/09/part2.py index 18ad5e6..2ceeb8e 100644 --- a/2024/09/part2.py +++ b/2024/09/part2.py @@ -23,31 +23,61 @@ def move_files_ltr_to_empty_space(files: list) -> str: while left < right: if not "." in files[left]: + #print("if ", end=" ") left += 1 + # TODO: Shouldn't we take care of this in the partial block? + elif len(files[right]) == 0: + del(files[right]) elif "." in files[right] \ or files[left].count(".") < len(files[right]): right -= 1 + #print("elif", end=" ") # fully swap elif files[left].count(".") == len(files[right]): + #print("part", end=" ") tmp = files[left] files[left] = files[right] files[right] = tmp + + # Restart trying from the right + right = len(files) - 1 + + # TODO: Concatenate empty space # partially swap else: + #print("part", end=" ") + #print(left, right) + #print(files[left], files[right]) + #print("." in files[right]) + #print(files[right]) tmp = files[left] files[left] = files[right] files.insert(left + 1, tmp[:-len(files[right])]) - files[right] = tmp[len(files[right])] + idx_right_after_insertion = right + 1 + files[idx_right_after_insertion] = tmp[:len(files[idx_right_after_insertion])] + if len(files[idx_right_after_insertion]) == 0: + #print("deleting") + del(files[idx_right_after_insertion]) + #print("RIGHT: ", files[right]) + + # Restart trying from the right + right = len(files) - 1 if verbose: - print(files) + print(files_to_blocks(files)) + #print(files) return files def files_to_blocks(files: list) -> str: + ret = "" for f in files: - print(f) - return sum(files) + ret += "".join(f) + #print(f) + return ret + # return ["".join(file) for file in files] + +#def blocks_to_str if __name__ == "__main__": disk_map = load_disk_map("testinput") @@ -59,7 +89,8 @@ if __name__ == "__main__": files = disk_map_to_files(disk_map) if verbose: print("Files:") - print(files) + #print(files) + print(files_to_blocks(files)) result = move_files_ltr_to_empty_space(files)