Luna Lailatova
2 years ago
4 changed files with 1239 additions and 13 deletions
@ -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) |
@ -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) |
@ -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 |
||||
|
Loading…
Reference in new issue