parent
74b2cc935f
commit
c6998260c6
@ -0,0 +1,23 @@
|
||||
$ cd /
|
||||
$ ls
|
||||
dir a
|
||||
14848514 b.txt
|
||||
8504156 c.dat
|
||||
dir d
|
||||
$ cd a
|
||||
$ ls
|
||||
dir e
|
||||
29116 f
|
||||
2557 g
|
||||
62596 h.lst
|
||||
$ cd e
|
||||
$ ls
|
||||
584 i
|
||||
$ cd ..
|
||||
$ cd ..
|
||||
$ cd d
|
||||
$ ls
|
||||
4060174 j
|
||||
8033020 d.log
|
||||
5626152 d.ext
|
||||
7214296 k
|
@ -0,0 +1,52 @@
|
||||
lines = open("input.txt", "r").read().splitlines()
|
||||
|
||||
|
||||
class Directory:
|
||||
def __init__(self, path, parent):
|
||||
self.path = path
|
||||
self.parent = parent
|
||||
self.children = []
|
||||
self.files = []
|
||||
|
||||
def mb(self):
|
||||
val_self = sum(self.files)
|
||||
for child in self.children:
|
||||
val_self = val_self + paths[child].mb()
|
||||
return val_self
|
||||
|
||||
|
||||
ls = False
|
||||
root = Directory("/", None)
|
||||
cwd = root
|
||||
paths = {"/": root}
|
||||
|
||||
for line in lines:
|
||||
cmd = line.split(" ")
|
||||
if cmd[0] == "$":
|
||||
ls = False
|
||||
if cmd[1] == "cd":
|
||||
if cmd[2] == "..":
|
||||
path = cwd.parent.path
|
||||
cwd = cwd.parent
|
||||
elif cmd[2] == "/":
|
||||
path = "/"
|
||||
cwd = root
|
||||
else:
|
||||
path = cwd.path + cmd[2] + "/"
|
||||
if path not in paths:
|
||||
paths[path] = Directory(path, cwd)
|
||||
cwd = paths[path]
|
||||
if cmd[1] == "ls":
|
||||
ls = True
|
||||
elif ls:
|
||||
if cmd[0] != "dir":
|
||||
cwd.files.append(int(cmd[0]))
|
||||
else:
|
||||
cwd.children.append(cwd.path + cmd[1] + "/")
|
||||
|
||||
sizes = []
|
||||
for dirs in paths.values():
|
||||
if dirs.mb() <= 100000:
|
||||
sizes.append(dirs.mb())
|
||||
|
||||
print(sum(sizes))
|
@ -0,0 +1,57 @@
|
||||
lines = open("input.txt", "r").read().splitlines()
|
||||
|
||||
|
||||
class Directory:
|
||||
def __init__(self, path, parent):
|
||||
self.path = path
|
||||
self.parent = parent
|
||||
self.children = []
|
||||
self.files = []
|
||||
|
||||
def mb(self):
|
||||
val_self = sum(self.files)
|
||||
for child in self.children:
|
||||
val_self = val_self + paths[child].mb()
|
||||
return val_self
|
||||
|
||||
|
||||
ls = False
|
||||
root = Directory("/", None)
|
||||
cwd = root
|
||||
paths = {"/": root}
|
||||
|
||||
for line in lines:
|
||||
cmd = line.split(" ")
|
||||
if cmd[0] == "$":
|
||||
ls = False
|
||||
if cmd[1] == "cd":
|
||||
if cmd[2] == "..":
|
||||
path = cwd.parent.path
|
||||
cwd = cwd.parent
|
||||
elif cmd[2] == "/":
|
||||
path = "/"
|
||||
cwd = root
|
||||
else:
|
||||
path = cwd.path + cmd[2] + "/"
|
||||
if path not in paths:
|
||||
paths[path] = Directory(path, cwd)
|
||||
cwd = paths[path]
|
||||
if cmd[1] == "ls":
|
||||
ls = True
|
||||
elif ls:
|
||||
if cmd[0] != "dir":
|
||||
cwd.files.append(int(cmd[0]))
|
||||
else:
|
||||
cwd.children.append(cwd.path + cmd[1] + "/")
|
||||
|
||||
sizes = {}
|
||||
for dirs in paths.values():
|
||||
if (70000000 - paths["/"].mb()) + dirs.mb() >= 30000000:
|
||||
sizes[dirs.path] = dirs.mb()
|
||||
|
||||
smallest = 70000000
|
||||
for i in sizes:
|
||||
if sizes[i] < smallest:
|
||||
smallest = sizes[i]
|
||||
|
||||
print(smallest)
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in new issue