Luna Lailatova 2 years ago
parent
commit
3a7a118704
  1. 85
      day7-1.py
  2. 91
      day7-2.py
  3. 36
      in.txt
  4. 1040
      input7.txt

85
day7-1.py

@ -0,0 +1,85 @@
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)

91
day7-2.py

@ -0,0 +1,91 @@
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)

36
in.txt

@ -1,13 +1,23 @@
[D] [N] [F] $ cd /
[H] [F] [L] [J] [H] $ ls
[R] [H] [F] [V] [G] [H] dir a
[Z] [Q] [Z] [W] [L] [J] [B] 14848514 b.txt
[S] [W] [H] [B] [H] [D] [C] [M] 8504156 c.dat
[P] [R] [S] [G] [J] [J] [W] [Z] [V] dir d
[W] [B] [V] [F] [G] [T] [T] [T] [P] $ cd a
[Q] [V] [C] [H] [P] [Q] [Z] [D] [W] $ ls
1 2 3 4 5 6 7 8 9 dir e
29116 f
move 1 from 3 to 9 2557 g
move 2 from 2 to 1 62596 h.lst
move 3 from 5 to 4 $ cd e
$ ls
584 i
$ cd ..
$ cd ..
$ cd d
$ ls
4060174 j
8033020 d.log
5626152 d.ext
7214296 k

1040
input7.txt

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save