my solutions for advent of code 2022
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.

86 lines
1.8 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
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:
print(i)
tempa, tempb = i.split(' ', 1)
if tempa.isnumeric():
curdir.newEntry(File(curdir, tempb, int(tempa)))
for i in _all:
if i.size() <= 100000:
answer += i.size()
print(answer)