-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday7.py
71 lines (60 loc) · 2.06 KB
/
day7.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
#!/usr/bin/env python
class directory():
def __init__(self, name, parent):
self.name = name
self.directories = {}
self.files = []
self.parent = parent
self.size = -1
def calculateSize(self):
if self.size >= 0:
return self.size
size = 0
for file in self.files:
size += file.size
for directoryName in self.directories:
size += self.directories[directoryName].calculateSize()
self.size = size
return self.size
class file():
def __init__(self, name, size):
self.name = name
self.size = size
f = open("ressources/day7.txt", "r")
lines = f.readlines()
root = directory("/", None)
currentDirectory = root
allDirectories = []
for line in lines[1:]:
instructions = line.replace("\n", "").split(" ")
if instructions[0] == '$':
if instructions[1] == 'cd':
if instructions[2] == '..':
currentDirectory = currentDirectory.parent
else:
currentDirectory = currentDirectory.directories[instructions[2]]
if instructions[1] == 'ls':
pass
else:
if instructions[0] == 'dir':
newDirectory = directory(instructions[1], currentDirectory)
currentDirectory.directories[instructions[1]] = newDirectory
allDirectories.append(newDirectory)
else:
newFile = file(instructions[1], int(instructions[0]))
currentDirectory.files.append(newFile)
root.calculateSize()
totalSize = 0
for dir in allDirectories:
if dir.size <= 100000:
totalSize += dir.size
print("Your Total Size of small files is: {}".format(totalSize))
occupiedSpace = root.size
requiredSpace = 30000000
availableSpace = 70000000
toDeleteSpace = root.size - (availableSpace-requiredSpace)
closestSpace = availableSpace
for dir in allDirectories:
if dir.size > toDeleteSpace and dir.size < closestSpace:
closestSpace = dir.size
print("You need to delete a directory of size: {}".format(closestSpace))