Luna Lailatova 1 year 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 @@ @@ -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 @@ @@ -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 @@ @@ -1,13 +1,23 @@
[D] [N] [F]
[H] [F] [L] [J] [H]
[R] [H] [F] [V] [G] [H]
[Z] [Q] [Z] [W] [L] [J] [B]
[S] [W] [H] [B] [H] [D] [C] [M]
[P] [R] [S] [G] [J] [J] [W] [Z] [V]
[W] [B] [V] [F] [G] [T] [T] [T] [P]
[Q] [V] [C] [H] [P] [Q] [Z] [D] [W]
1 2 3 4 5 6 7 8 9
move 1 from 3 to 9
move 2 from 2 to 1
move 3 from 5 to 4
$ 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

1040
input7.txt

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