Advertisement
Guest User

AoC 2022 Day 7

a guest
Dec 10th, 2022
1,305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.61 KB | None | 0 0
  1. # aoc202207.py
  2.  
  3. import pathlib
  4. import sys
  5.  
  6.  
  7. def parse(puzzle_input: str) -> list[list[str]]:
  8.     """ Parse input """
  9.     data = puzzle_input.replace('$ ', '').splitlines()
  10.     return [line.split(' ') for line in data]
  11.  
  12.  
  13. def folder_sizes(output: list[list[str]]) -> dict[str,int]:
  14.     """ Return a dict of folder sizes from commands """
  15.     folders: dict[str, int] = {}
  16.     path: list[str] = ['/']
  17.     current_path: str = ''.join(path)
  18.     folders.setdefault(current_path, 0)
  19.  
  20.     for line in output:
  21.         if line[0] == "ls":
  22.             continue
  23.         elif line[0] == "dir":
  24.             folders.setdefault(current_path + line[1] + '/', 0)
  25.         elif line[0].isdigit():
  26.             folders[current_path] += int(line[0])
  27.         elif line[0] == "cd":
  28.             if line[1] == "..":
  29.                 # Account for subfolder size
  30.                 subfolder_size: int = folders.get(current_path)
  31.                 path.pop()
  32.                 current_path = ''.join(path)
  33.                 folders[current_path] += subfolder_size
  34.             elif line[1] == '/':
  35.                 path = [line[1]]
  36.                 current_path = ''.join(path)
  37.             else:
  38.                 path.append(line[1] + '/')
  39.                 current_path = ''.join(path)
  40.  
  41.     # Add folder size for any remaining folders in stack
  42.     for n in range(len(path) - 1):
  43.         subfolder_size = folders.get(current_path)
  44.         path.pop()
  45.         current_path = ''.join(path)
  46.         folders[current_path] += subfolder_size
  47.  
  48.     return folders
  49.  
  50.  
  51. def part1(data: list[list[str]]) -> int:
  52.     """ Solve part 1 """
  53.     dirs: dict[str, int] = folder_sizes(data[1:])
  54.  
  55.     return sum(v for v in dirs.values() if v <= 100000)
  56.  
  57.  
  58. def part2(data: list[list[str]]) -> int:
  59.     """ Solve part 2 """
  60.     dirs: dict[str, int] = folder_sizes(data[1:])
  61.     total: int = 70000000
  62.     space_needed: int = 30000000
  63.     space_used: int = dirs.get('/')
  64.     unused_space: int = total - space_used
  65.  
  66.     return min(v for v in dirs.values() if v > (space_needed - unused_space))
  67.  
  68.  
  69. def solve(puzzle_input: str) -> tuple[int, int]:
  70.     """ Solve the puzzle for the given input """
  71.     data = parse(puzzle_input)
  72.     solution1: int = part1(data)  # Correct answer was 1723892 (with my data)
  73.     solution2: int = part2(data)  # Correct answer was 8474158 (with my data)
  74.  
  75.     return solution1, solution2
  76.  
  77.  
  78. if __name__ == "__main__":
  79.     for path in sys.argv[1:]:
  80.         print(f"{path}:")
  81.         puzzle_input = pathlib.Path(path).read_text().strip()
  82.         solutions = solve(puzzle_input)
  83.         print('\n'.join(str(solution) for solution in solutions))
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement