You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
1.9 KiB
92 lines
1.9 KiB
2 years ago
|
import abc
|
||
|
|
||
|
with open('input7.txt','r') as f:
|
||
|
inp = f.read().splitlines(keepends=False)
|
||
|
|
||
|
class FSObject:
|
||
|
__metaclass__ = abc.ABCMeta
|
||
|
|
||
|
def __init__(self, parent):
|
||
|
self._parent = parent
|
||
|
|
||
|
@abc.abstractmethod
|
||
|
def name(self):
|
||
|
pass
|
||
|
|
||
|
@abc.abstractmethod
|
||
|
def size(self):
|
||
|
pass
|
||
|
|
||
|
def fullpath(self):
|
||
|
if self._parent:
|
||
|
return self._parent.fullpath() + "/" + self.name()
|
||
|
else:
|
||
|
return self.name()
|
||
|
|
||
|
|
||
|
class File(FSObject):
|
||
|
def __init__(self, parent, name, size):
|
||
|
super(File, self).__init__(parent)
|
||
|
self._name = name
|
||
|
self._size = size
|
||
|
|
||
|
def size(self):
|
||
|
return self._size
|
||
|
|
||
|
class Directory(FSObject):
|
||
|
def __init__(self, parent, name):
|
||
|
super(Directory, self).__init__(parent)
|
||
|
self._name = name
|
||
|
self.contain = []
|
||
|
|
||
|
def newEntry(self, obj):
|
||
|
self.contain.append(obj)
|
||
|
|
||
|
def size(self):
|
||
|
_size = 0
|
||
|
for x in self.contain:
|
||
|
_size += x.size()
|
||
|
return _size
|
||
|
|
||
|
|
||
|
location = []
|
||
|
rootdir = Directory(None, "")
|
||
|
curdir = rootdir
|
||
|
dirs = [rootdir]
|
||
|
_all = []
|
||
|
answer = 0
|
||
|
totalSpace = 70000000
|
||
|
requiredSpace = 30000000
|
||
|
opt = None
|
||
|
|
||
|
for i in inp:
|
||
|
if i.startswith('$ cd'):
|
||
|
target = i[5:]
|
||
|
if target == '..':
|
||
|
location.pop()
|
||
|
dirs.pop()
|
||
|
curdir = dirs[-1]
|
||
|
else:
|
||
|
location.append(target)
|
||
|
newdir = Directory(curdir, target)
|
||
|
_all.append(newdir)
|
||
|
curdir.newEntry(newdir)
|
||
|
dirs.append(newdir)
|
||
|
curdir = newdir
|
||
|
elif i.startswith('$ ls'):
|
||
|
pass
|
||
|
else:
|
||
|
tempa, tempb = i.split(' ', 1)
|
||
|
if tempa.isnumeric():
|
||
|
curdir.newEntry(File(curdir, tempb, int(tempa)))
|
||
|
|
||
|
freeSpace = totalSpace - rootdir.size()
|
||
|
toDelete = requiredSpace - freeSpace
|
||
|
|
||
|
for i in _all:
|
||
|
tempa = i.size()
|
||
|
if tempa >= toDelete and ((not opt) or tempa < opt):
|
||
|
opt = tempa
|
||
|
|
||
|
print(opt)
|