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
dir a
14848514 b.txt
@ -28,14 +30,18 @@ $ ls
"""
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 = [[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)
def recursive_parse(outputs:list):
def recursive_parse(outputs: list):
structure = {}
while True:
try:
@ -50,60 +56,64 @@ def recursive_parse(outputs:list):
structure[name] = int(value) if value.isnumeric() else value
elif command.startswith('cd'):
dest = command.split()[1]
if dest == '..':
destination = command.split()[1]
if destination == '..':
return structure
else:
structure[dest] = recursive_parse(outputs)
structure[destination] = recursive_parse(outputs)
def recursive_sum(d:dict, folders:list=None, initial=True):
s=0
folders = [] if folders==None else folders
def recursive_sum(d: dict, folders: list = None, initial=True):
s = 0
folders = [] if folders is None else folders
for name, value in d.items():
if isinstance(value, dict):
zs = recursive_sum(value, folders, False)
folders.append((name,zs))
folders.append((name, zs))
else:
zs = value
s+=zs
s += zs
if initial:
return folders
else:
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):
for k,v in d.items():
if isinstance( v, dict):
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):
for k, v in d.items():
if isinstance(v, dict):
print(' '*indent, 'dir', k)
recursive_print(v, indent+increment)
else:
print(' '*indent, k,v)
print(' '*indent, k, v)
system = parse_stdin(text)
# recursive_print(system)
sums = recursive_sum(system)
l = folders_up_to_size(sums,100000)
print(l)
print(sum([v for _,v in l]))
summed_folders = folders_up_to_size(sums, 100000)
print(summed_folders)
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):
return [(k,v) for k,v in folders if v>size]
def folders_over_size(folders: list, size: int):
return [(k, v) for k, v in folders if v > size]
total = sums[-1][1]
over = total-40000000
print('filesystem overfull by:', over)
l = folders_over_size(sums,over)
l.sort(key = lambda x: x[1])
print(l[0])
summed_folders = folders_over_size(sums, over)
summed_folders.sort(key=lambda x: x[1])
print(summed_folders[0])