-
Notifications
You must be signed in to change notification settings - Fork 0
/
07-part1.py
85 lines (78 loc) · 1.92 KB
/
07-part1.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/python3
import fileinput
import sys
import re
def add_entry(data,parent):
fileregex = re.compile('^([0-9]+) ([a-zA-Z0-9\.]+)$')
dirregex = re.compile('^dir ([a-zA-Z0-9\.]+)$')
fileresult = fileregex.match(data)
dirresult = dirregex.match(data)
if fileresult:
size = int(fileresult[1])
name = fileresult[2]
mytype = 'file'
elif dirresult:
size = 0
name = dirresult[1]
mytype = 'dir'
else:
raise Exception("error")
target = {
'parent': parent,
'children': [],
'name': name,
'type': mytype,
'size': size
}
parent['children'].append(target)
def get_child_by_name(name,parent):
for i in parent['children']:
if i['name'] == name:
return i
return None
def find_child_directories(parent):
mylist = []
for i in parent['children']:
if i['type'] == 'dir':
mylist.append(i)
mylist.extend(find_child_directories(i))
return mylist
def find_dir_size(dir):
total = 0
for i in dir['children']:
if i['type'] == 'dir':
total += find_dir_size(i)
else:
total += i['size']
return total
root = {
'parent': None,
'children': [],
'name': '',
'type': 'dir',
'size': 0
}
cwd = root
cdregex = re.compile('^cd ([a-zA-Z0-9\.]+)$')
ls_data = []
for line in fileinput.input():
line = line.rstrip('\n')
if line == '$ cd ..':
cwd = cwd['parent']
elif line[0:4] == '$ cd':
if line[5:] == "/":
cwd = root
else:
cwd = get_child_by_name(line[5:],cwd)
if cwd == None:
Exception("error cwd")
elif line == '$ ls':
continue
else:
add_entry(line,cwd)
total = 0
for i in find_child_directories(root):
mysize = find_dir_size(i)
if mysize <= 100000:
total += mysize
print(total)