Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 13fbd72

Browse files
author
mhamid
committed
day 7
1 parent 81e00f9 commit 13fbd72

File tree

3 files changed

+1172
-0
lines changed

3 files changed

+1172
-0
lines changed

‎day7/day7.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import os
2+
3+
# read input
4+
input = open('dummy.txt', 'r')
5+
lines = input.readlines()
6+
directories = {}
7+
directorySize = {}
8+
fileSystem = {}
9+
totalSize = 0
10+
11+
# apply
12+
for line in lines:
13+
line = line.split('\n')[0]
14+
if (line[0] == '$'):
15+
ds, command, *directory = line.split()
16+
if (command == 'cd'):
17+
path = directory[0]
18+
if (path == '/'):
19+
currentDirectory = path
20+
else:
21+
currentDirectory = os.path.normpath(os.path.join(currentDirectory, path))
22+
if (currentDirectory not in directories):
23+
directories[currentDirectory] = []
24+
fileSystem[currentDirectory] = {
25+
"path": [],
26+
"size": 0,
27+
"files": {
28+
"name": [],
29+
"size": []
30+
}
31+
}
32+
directorySize[currentDirectory] = 0
33+
else:
34+
fileSize, fileName = line.split()
35+
if (fileSize != 'dir'):
36+
directorySize[currentDirectory] += int(fileSize)
37+
fileSystem[currentDirectory]['size'] += int(fileSize)
38+
fileSystem[currentDirectory]['files']['name'].append(fileName)
39+
fileSystem[currentDirectory]['files']['size'].append(int(fileSize))
40+
else:
41+
directories[currentDirectory].append(os.path.normpath(os.path.join(currentDirectory, fileName)))
42+
fileSystem[currentDirectory]['path'].append(os.path.normpath(os.path.join(currentDirectory, fileName)))
43+
44+
# compute directory sizes
45+
def computeDirectorySize(dirName: str):
46+
dirSize = directorySize[dirName]
47+
for item in directories[dirName]:
48+
if item in directories:
49+
dirSize += computeDirectorySize(item)
50+
return dirSize
51+
52+
for item in directories:
53+
dirSize = computeDirectorySize(item)
54+
if dirSize <= 100000:
55+
totalSize += dirSize
56+
57+
# second way
58+
total = 0
59+
def compute(name):
60+
size = fileSystem[name]['size']
61+
for i in fileSystem[name]['path']:
62+
if i in fileSystem:
63+
size += compute(i)
64+
return size
65+
66+
for key in fileSystem:
67+
size = compute(key)
68+
if size <= 100000:
69+
total += size
70+
71+
# print(fileSystem)
72+
# print(directories)
73+
# print(directorySize)
74+
print(totalSize)
75+
print(total)

‎day7/dummy.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
$ cd /
2+
$ ls
3+
dir a
4+
14848514 b.txt
5+
8504156 c.dat
6+
dir d
7+
$ cd a
8+
$ ls
9+
dir e
10+
29116 f
11+
2557 g
12+
62596 h.lst
13+
$ cd e
14+
$ ls
15+
584 i
16+
$ cd ..
17+
$ cd ..
18+
$ cd d
19+
$ ls
20+
4060174 j
21+
8033020 d.log
22+
5626152 d.ext
23+
7214296 k

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /