Advertisement
Guest User

AoC Day 7 Solution

a guest
Dec 9th, 2022
767
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.54 KB | Software | 0 0
  1. """
  2. Written by nmgetahun
  3. Github username: nmgetahun
  4.  
  5. Advent of Code Day 7 Challenge:
  6. https://adventofcode.com/2022/day/7
  7. """
  8. # ------------------------------------------------------------------------------
  9. import json
  10.  
  11. # Part 1 Primary
  12. # Store home directory into dictionary
  13. def add_directory(file, size):
  14.     dir = {}
  15.     line = next_line(file)
  16.  
  17.     while line[0] != "$":
  18.         dir[line[1]] = {} if line[0] == "dir" else int(line[0])
  19.         line = next_line(file)
  20.  
  21.     while line[2] != "..":
  22.         if line[1] != "ls":
  23.             dir[line[2]] = add_directory(next_line(file, True), size)
  24.  
  25.             # Part 1
  26.             dir_size = get_size(dir[line[2]])
  27.             size[0] += dir_size if dir_size <= 100000 else 0
  28.  
  29.         line = next_line(file)
  30.  
  31.     return dir
  32.  
  33.  
  34. # Part 1 Helper
  35. # Get OR skip next line in input file; terminates at file end
  36. def next_line(file, skip = False):
  37.     line = file.readline().strip().split()
  38.     return file if skip else line if line != [] else ["$", "cd", ".."]
  39.  
  40.  
  41. # Parts 1 & 2 Helper
  42. # Get total size of a directory
  43. def get_size(dir):
  44.     size = 0
  45.     for sub_dir in dir.values():
  46.         size += get_size(sub_dir) if type(sub_dir) == dict else sub_dir
  47.  
  48.     return size
  49.  
  50.  
  51. # Part 2 Primary
  52. # Find smallest directory (size AND name) with enough space to create minimum unused space if deleted
  53. def find_optimal_directory(dir, size, optimal_directory):
  54.     for name, sub_dir in dir.items():
  55.         if type(sub_dir) == dict:
  56.             optimal_directory = find_optimal_directory(sub_dir, size, optimal_directory)
  57.             sub_dir_size = get_size(sub_dir)
  58.  
  59.             if sub_dir_size > size and sub_dir_size < optimal_directory[0]:
  60.                 optimal_directory = [sub_dir_size, name]
  61.  
  62.     return optimal_directory
  63.  
  64.  
  65. # Main
  66. if __name__ == "__main__":
  67.     # Get data from today's AoC input file + Part 1
  68.     with open("aoc7input.txt") as file:
  69.         small_dirs = [0] # Total size of dirs <= 100000
  70.         home = {next_line(file)[2]: add_directory(next_line(file, True), small_dirs)}
  71.         print(json.dumps(home, indent = 16)) # Print home directory (pretty though)
  72.  
  73.     # Part 2
  74.     TOTAL_DISK_SPACE = 70000000
  75.     MINIMUM_UNUSED_SPACE = 30000000
  76.     home_size = get_size(home)
  77.  
  78.     space_needed = MINIMUM_UNUSED_SPACE - TOTAL_DISK_SPACE + home_size
  79.     optimal_directory = find_optimal_directory(home, space_needed, [home_size, None]) # Size, name
  80.  
  81.     # Display
  82.     print("Part 1: ", small_dirs[0], "\nPart 2: ", optimal_directory[0])
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement