Advertisement
nairby

2022 Day 07

Dec 7th, 2022 (edited)
904
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.86 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import sys
  4.  
  5. PART1_SIZE      = 100000
  6. TOTAL_DISKSPACE = 70000000
  7. PART2_TARGET    = 30000000
  8.  
  9. class Dir(object):
  10.     def __init__(self,name,parent):
  11.         self.name = name
  12.         self.parent = parent
  13.         self.contents = []
  14.  
  15.     def size(self):
  16.         return sum([item.size() for item in self.contents])
  17.  
  18.     def full_path(self):
  19.         if self.parent is None: return self.name
  20.         return self.name+"/"+self.parent.full_path()
  21.  
  22. class File(object):
  23.     def __init__(self,name,parent,size):
  24.         self.name = name
  25.         self.parent = parent
  26.         self._size = size
  27.  
  28.     def size(self):
  29.         return self._size
  30.  
  31.     def __repr__(self):
  32.         return f"{self.name} ({self.size})"
  33.  
  34. def dirsum(d):
  35.     size = 0
  36.     for subdir in d.contents:
  37.         if type(subdir) == Dir: size += dirsum(subdir)
  38.     if d.size() <= PART1_SIZE: size += d.size()
  39.     return size
  40.  
  41. def smallest_dir_larger_than(d,minsize,bestsize):
  42.     new_best = bestsize
  43.     newsize = d.size()
  44.     if newsize >= minsize and newsize < bestsize:
  45.         new_best = newsize
  46.     for subdir in d.contents:
  47.         if type(subdir) == Dir:
  48.             new_best = smallest_dir_larger_than(subdir,minsize,new_best)
  49.     return new_best
  50.  
  51. def main():
  52.     # Input
  53.     args = sys.argv
  54.     input_file = args[1]
  55.     input_file = file_lines(input_file)
  56.  
  57.     # Generate filesystem
  58.     filesystem = Dir("/",None)
  59.     pwd = filesystem
  60.     for cmd in input_file:
  61.         if cmd[0] == "$":
  62.             # Issue command
  63.             cmd = cmd[2:]
  64.             if cmd == "cd /":
  65.                 pwd = filesystem
  66.             elif cmd == "cd ..":
  67.                 pwd = pwd.parent
  68.             elif cmd == "ls":
  69.                 pass
  70.             else:
  71.                 if cmd.split(" ")[0] == "cd":
  72.                     target = cmd.split(" ")[1]
  73.                     pwd = next((x for x in pwd.contents if x.name == target and type(x) == Dir), None)
  74.                 else:
  75.                     quit(f"{cmd}: command not found...")
  76.  
  77.         else:
  78.             # ls output
  79.             parts = cmd.split(" ")
  80.             if parts[0] == "dir":
  81.                 # dir
  82.                 dirname = parts[1]
  83.                 pwd.contents.append(Dir(dirname,pwd))
  84.             else:
  85.                 # file
  86.                 filename,sz = parts[1],int(parts[0])
  87.                 pwd.contents.append(File(filename,pwd,sz))
  88.  
  89.     # Part 1
  90.     print("Part 1: ",dirsum(filesystem)) # 1770595
  91.  
  92.     # Part 2
  93.     total_used = filesystem.size()
  94.     available = TOTAL_DISKSPACE - total_used
  95.     minimum_free = PART2_TARGET - available
  96.     print("Part 2: ",smallest_dir_larger_than(filesystem,minimum_free,TOTAL_DISKSPACE)) # 2195372
  97.  
  98. def file_lines(file):
  99.     with open(file) as f:
  100.         data = [line.rstrip() for line in f]
  101.     return data
  102.  
  103. if __name__ == "__main__":
  104.     main()
  105.  
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement