bow to pycharm's linting and nagging ...

This commit is contained in:
Daniel Mevec 2022-12-07 14:52:52 +01:00
parent cdd5b41dfa
commit fd148c0263

View file

@ -1,8 +1,10 @@
""" """
parse commandline output, reconstruct a fileststem and determine the sum of all folders < 100kB parse commandline output, reconstruct a filesystem and determine the sum of all folders < 100kB
""" """
text = """$ cd / TESTRUN = True
test_text = """$ cd /
$ ls $ ls
dir a dir a
14848514 b.txt 14848514 b.txt
@ -28,14 +30,18 @@ $ ls
""" """
with open('2022/7/stdout.txt') as filein: with open('2022/7/stdout.txt') as filein:
text = filein.read() input_text = filein.read()
def parse_stdin(stdin:str): text = test_text if TESTRUN else input_text
def parse_stdin(stdin: str):
outputs = stdin.split('$') outputs = stdin.split('$')
outputs = [[l.strip() for l in o.splitlines()] for o in outputs if o] outputs = [[line.strip() for line in output.splitlines()] for output in outputs if output]
return recursive_parse(outputs) return recursive_parse(outputs)
def recursive_parse(outputs:list):
def recursive_parse(outputs: list):
structure = {} structure = {}
while True: while True:
try: try:
@ -50,60 +56,64 @@ def recursive_parse(outputs:list):
structure[name] = int(value) if value.isnumeric() else value structure[name] = int(value) if value.isnumeric() else value
elif command.startswith('cd'): elif command.startswith('cd'):
dest = command.split()[1] destination = command.split()[1]
if dest == '..': if destination == '..':
return structure return structure
else: else:
structure[dest] = recursive_parse(outputs) structure[destination] = recursive_parse(outputs)
def recursive_sum(d:dict, folders:list=None, initial=True):
s=0 def recursive_sum(d: dict, folders: list = None, initial=True):
folders = [] if folders==None else folders s = 0
folders = [] if folders is None else folders
for name, value in d.items(): for name, value in d.items():
if isinstance(value, dict): if isinstance(value, dict):
zs = recursive_sum(value, folders, False) zs = recursive_sum(value, folders, False)
folders.append((name,zs)) folders.append((name, zs))
else: else:
zs = value zs = value
s+=zs s += zs
if initial: if initial:
return folders return folders
else: else:
return s return s
def folders_up_to_size(folders:list, size:int):
return [(k,v) for k,v in folders if v<=size]
def recursive_print(d:dict, indent = 0, increment=2): def folders_up_to_size(folders: list, size: int):
for k,v in d.items(): return [(k, v) for k, v in folders if v <= size]
if isinstance( v, dict):
def recursive_print(d: dict, indent=0, increment=2):
for k, v in d.items():
if isinstance(v, dict):
print(' '*indent, 'dir', k) print(' '*indent, 'dir', k)
recursive_print(v, indent+increment) recursive_print(v, indent+increment)
else: else:
print(' '*indent, k,v) print(' '*indent, k, v)
system = parse_stdin(text) system = parse_stdin(text)
# recursive_print(system) # recursive_print(system)
sums = recursive_sum(system) sums = recursive_sum(system)
l = folders_up_to_size(sums,100000) summed_folders = folders_up_to_size(sums, 100000)
print(l) print(summed_folders)
print(sum([v for _,v in l])) print(sum([v for _, v in summed_folders]))
""" """
now find the smallest folser that can be deleted to free up the space needed to keep the total size under 40MB now find the smallest folder that can be deleted to free up the space needed to keep the total size under 40MB
""" """
def folders_over_size(folders:list, size:int): def folders_over_size(folders: list, size: int):
return [(k,v) for k,v in folders if v>size] return [(k, v) for k, v in folders if v > size]
total = sums[-1][1] total = sums[-1][1]
over = total-40000000 over = total-40000000
print('filesystem overfull by:', over) print('filesystem overfull by:', over)
l = folders_over_size(sums,over) summed_folders = folders_over_size(sums, over)
l.sort(key = lambda x: x[1]) summed_folders.sort(key=lambda x: x[1])
print(l[0]) print(summed_folders[0])