-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7.py
57 lines (55 loc) · 1.33 KB
/
7.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
import fetch_input
import os
import re
lines = list(fetch_input.get_input(7))
def group_string(string, line_len):
while len(string) > 0:
yield string[:line_len]
string = string[line_len:]
i = 0
curdir = None
dirs = {}
subdirs = {}
for line in lines:
if len(line.strip()) == 0: continue
if line[0] == '$':
c, cmd, *args = line.split()
if cmd == 'cd':
path ,= args
if path[0] == '/':
curdir = path
else:
curdir = os.path.normpath(os.path.join(curdir, path))
if curdir not in dirs:
dirs[curdir] = 0
subdirs[curdir] = []
else:
sz, fname = line.split()
if sz != 'dir':
dirs[curdir] += int(sz)
else:
subdirs[curdir].append(os.path.normpath(os.path.join(curdir, fname)))
dirsizes = {}
def dirsize(dirname):
dsize = dirs[dirname]
for i in subdirs[dirname]:
if i in dirs:
dsize += dirsize(i)
return dsize
totsize = 0
# part 1
for d in dirs:
dsize = dirsize(d)
if dsize <= 100000:
totsize += dsize
print(totsize)
# part 2
totsize = dirsize('/')
unused = 70000000 - totsize
ms = None
for d in dirs:
ds = dirsize(d)
if unused + ds >= 30000000:
if ms is None or ms > ds:
ms = ds
print(ms)