Guest User

Untitled

a guest
Dec 7th, 2022
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.51 KB | None | 0 0
  1. import re
  2. from itertools import chain
  3.  
  4.  
  5. class Directory:
  6.  
  7.     def __init__(self, identity, parent=None):
  8.         self.identity = identity
  9.         self.contents = []
  10.         self.parent = parent
  11.         self.size = 0
  12.         if self.identity == "/":
  13.             Directory.root = self
  14.  
  15.     def add(self, identity, type, parent, size=None):
  16.         if type == 'file':
  17.             self.contents.append(File(identity, size))
  18.         else:
  19.             self.contents.append(Directory(identity, self))
  20.         size = self.get_size()
  21.  
  22.     def get_size(self):
  23.         size = 0
  24.         for item in self.contents:
  25.             if isinstance(item, File):
  26.                 size += int(item.size)
  27.             else:
  28.                 size += int(item.get_size())
  29.         return size
  30.  
  31.     def iterable_of_sizes(self):
  32.         children = filter(lambda thing: isinstance(thing, Directory),
  33.                           self.contents)
  34.         return chain([self.get_size()],
  35.                      *[child.iterable_of_sizes() for child in children])
  36.  
  37.     def cd(self, command):
  38.         if command == '..':
  39.             return self.parent
  40.         elif command == '/':
  41.             return Directory(command)
  42.         else:
  43.             for i in self.contents:
  44.                 if i.identity == command:
  45.                     return i
  46.  
  47.  
  48. class File:
  49.  
  50.     def __init__(self, identity, size):
  51.         self.identity = identity
  52.         self.size = size
  53.  
  54.     def iterable_of_sizes(self):
  55.         return chain([self.size])
  56.  
  57.  
  58. with open("input_7.txt", "r+")as input_file:
  59.     blocks = [x.rstrip().split("\n") for x in re.split(r"(?=\$)", input_file.read())]
  60.     location = None
  61.     for block in blocks:
  62.         if block[0] == "$ ls":
  63.             for entry in block[1:]:
  64.                     if entry.split()[0].isnumeric():
  65.                         location.add(entry.split()[-1], 'file', location, entry.split()[0])
  66.                     else:
  67.                         location.add(entry.split()[-1], 'directory', location)
  68.         elif "$ cd" in block[0]:
  69.             directory = block[0].split()[-1]
  70.             if not location:
  71.                 location = Directory(directory)
  72.             else:
  73.                 location = location.cd(directory)
  74.     disk_space = 70000000 - list(Directory.root.iterable_of_sizes())[0]
  75.     day_one = sum([x for x in list(Directory.root.iterable_of_sizes()) if x < 100000])
  76.     day_two = min([x for x in list(Directory.root.iterable_of_sizes()) if (disk_space + x) > 30000000])
  77.     print(day_one, day_two, sep="\n")
Advertisement
Add Comment
Please, Sign In to add comment