-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7-2.py
More file actions
45 lines (39 loc) · 1.22 KB
/
7-2.py
File metadata and controls
45 lines (39 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# python 7-2.py input7.txt
import sys
f = open(sys.argv[1])
system = dict() # { path: (dirs, files_size) }
command = f.readline()[:-1]
while command:
if command.startswith("$ cd"):
directory = command[5:] # Remove '$ cd'
if directory == "..":
current_path.pop()
elif directory == "/":
current_path = []
else:
current_path.append(directory)
command = f.readline()[:-1]
else: # $ ls
dirs = []
files_size = 0
command = f.readline()[:-1]
while command and not command.startswith("$ "):
first, second = command.split()
if first == "dir":
dirs.append("/".join(current_path + [second]))
else:
files_size += int(first)
command = f.readline()[:-1]
system["/".join(current_path)] = (dirs, files_size)
dir_sizes = dict()
# start by the last explored dir
for path in reversed(system.keys()):
dirs, dir_size = system[path]
for d in dirs:
dir_size += dir_sizes[d]
dir_sizes[path] = dir_size
unused = 70000000 - dir_sizes[""]
for size in sorted(dir_sizes.values()):
if size + unused > 30000000:
print(size)
break