-
Notifications
You must be signed in to change notification settings - Fork 0
/
a07.py
67 lines (54 loc) · 1.76 KB
/
a07.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
lines = open("07.txt").read().splitlines()
files = {"name":"/", "size":0, "isFolder":True, "children":[]}
curr = files
def addChild(parent, name, isFolder, size):
child = {"name":name, "size":size, "isFolder":isFolder, "children":[], "parent": parent}
parent["children"].append(child)
return child
def updateFolderSizes(parent):
size = 0
folders = [c for c in parent["children"] if c["isFolder"]]
for f in folders: updateFolderSizes(f)
for child in parent["children"]: size += child["size"]
parent["size"] = size
for row in lines:
a,b,c = (row.split(" ")+ [""])[0:3]
if a == "$":
if b == "cd":
if c == "..":
curr = curr["parent"]
elif c == "/":
pass
else:
curr = addChild(curr, c, True, None)
elif b == "ls":
pass
else:
if a == "dir":
pass
elif a.isdigit():
addChild(curr, b, False, int(a))
updateFolderSizes(files)
def part1(root):
size = 0
folders = [root]
while len(folders) > 0:
parent = folders.pop()
folders = folders + [c for c in parent["children"] if c["isFolder"]]
if parent["size"] <= 100000:
size = size + parent["size"]
return size
def part2(root, target):
bestSize = root["size"]
folders = [root]
while len(folders) > 0:
parent = folders.pop()
folders = folders + [c for c in parent["children"] if c["isFolder"]]
availableAfterDelete = 70000000 - root["size"] + parent["size"]
if availableAfterDelete >= 30000000 and parent["size"] < bestSize:
bestSize = parent["size"]
return bestSize
p1 = part1(files)
print("p1", p1)
p2 = part2(files, p1)
print("p2", p2)