bow to pycharm's linting and nagging ...
This commit is contained in:
parent
cdd5b41dfa
commit
fd148c0263
1 changed files with 39 additions and 29 deletions
40
2022/7/7.py
40
2022/7/7.py
|
|
@ -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,13 +30,17 @@ $ ls
|
|||
"""
|
||||
|
||||
with open('2022/7/stdout.txt') as filein:
|
||||
text = filein.read()
|
||||
input_text = filein.read()
|
||||
|
||||
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):
|
||||
structure = {}
|
||||
while True:
|
||||
|
|
@ -50,15 +56,16 @@ 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
|
||||
folders = [] if folders is None else folders
|
||||
|
||||
for name, value in d.items():
|
||||
if isinstance(value, dict):
|
||||
|
|
@ -73,9 +80,11 @@ def recursive_sum(d:dict, folders:list=None, initial=True):
|
|||
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):
|
||||
|
|
@ -88,22 +97,23 @@ def recursive_print(d:dict, indent = 0, increment=2):
|
|||
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]
|
||||
|
||||
|
||||
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])
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue