-
Notifications
You must be signed in to change notification settings - Fork 0
/
one.py
46 lines (39 loc) · 1.36 KB
/
one.py
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
46
class Directory:
def __init__(self, name, parent):
self.name = name
self.parent = parent
self.files = {}
self.subdirs = {}
self._size = None
def size(self):
if not self._size:
self._size = sum(self.files.values()) + sum(subdir.size() for subdir in self.subdirs.values())
return self._size
def size_generator(self):
for subdir in self.subdirs.values():
yield from subdir.size_generator()
yield self._size
def parse_listing(filename):
current = root = Directory("", None)
current.subdirs["/"] = Directory("/", root)
with open(filename) as fileh:
for line in fileh:
parts = line.strip().split()
if parts[0] == "$" and parts[1] == "cd":
if parts[2] == "..":
current = current.parent
else:
current = current.subdirs[parts[2]]
elif parts[1] == "ls":
pass
elif parts[0] == "dir":
current.subdirs[parts[1]] = Directory(parts[1], current)
else:
current.files[parts[1]] = int(parts[0])
return root
def main(filename):
root = parse_listing(filename)
root.size()
return sum(s for s in root.size_generator() if s <= 100000)
print(main("example"))
print(main("input"))