2025(9): fix test, but fail normal on

This commit is contained in:
2025-02-08 18:52:50 +01:00
parent 110c6303b0
commit ef33a04975

View File

@ -12,7 +12,7 @@ def disk_map_to_files(disk_map: str) -> list:
files += [int(s) * [str(ID)]] files += [int(s) * [str(ID)]]
# free space # free space
else: elif int(s) > 0:
files += [int(s) * "."] files += [int(s) * "."]
return files return files
@ -21,28 +21,40 @@ def move_files_ltr_to_empty_space(files: list) -> str:
left = 0 left = 0
right = len(files) - 1 right = len(files) - 1
while left < right: while 0 < right:
#print(files)
#print(left, right)
# Concatenate empty space
while "." in files[left] and "." in files[left + 1]:
files[left] += files[left + 1]
del(files[left + 1])
right -= 1
if not left < right:
right -= 1
left = 0
continue
if not "." in files[left]: if not "." in files[left]:
#print("if ", end=" ") #print("if ", end=" ")
left += 1 left += 1
# TODO: Shouldn't we take care of this in the partial block? elif "." in files[right]:
elif len(files[right]) == 0:
del(files[right])
elif "." in files[right] \
or files[left].count(".") < len(files[right]):
right -= 1 right -= 1
#print("elif", end=" ") #print("elif", end=" ")
elif files[left].count(".") < len(files[right]):
left += 1
continue
# fully swap # fully swap
elif files[left].count(".") == len(files[right]): elif files[left].count(".") == len(files[right]):
#print("part", end=" ") #print("full", end=" ")
tmp = files[left] tmp = files[left]
files[left] = files[right] files[left] = files[right]
files[right] = tmp files[right] = tmp
# Restart trying from the right # Restart trying from the left
right = len(files) - 1 left = 0
right -= 1
# TODO: Concatenate empty space
# partially swap # partially swap
else: else:
#print("part", end=" ") #print("part", end=" ")
@ -55,13 +67,14 @@ def move_files_ltr_to_empty_space(files: list) -> str:
files.insert(left + 1, tmp[:-len(files[right])]) files.insert(left + 1, tmp[:-len(files[right])])
idx_right_after_insertion = right + 1 idx_right_after_insertion = right + 1
files[idx_right_after_insertion] = tmp[:len(files[idx_right_after_insertion])] files[idx_right_after_insertion] = tmp[:len(files[idx_right_after_insertion])]
if len(files[idx_right_after_insertion]) == 0: if len(files[right]) == 0:
#print("deleting")
del(files[idx_right_after_insertion]) del(files[idx_right_after_insertion])
right -= 1
#print("RIGHT: ", files[right]) #print("RIGHT: ", files[right])
# Restart trying from the right # Restart trying from the left
right = len(files) - 1 left = 0
if verbose: if verbose:
print(files_to_blocks(files)) print(files_to_blocks(files))