Guest User

Untitled

a guest
Dec 7th, 2022
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. from collections import defaultdict
  3. import copy
  4. from functools import lru_cache
  5. from string import ascii_lowercase, ascii_uppercase
  6. import os
  7. import numpy as np
  8.  
  9. os.chdir("/Users/Me/Desktop/AdventOfCode/07/")
  10. fn = "example.txt"
  11. # fn = "input.txt"
  12. # data = open(fn).read().strip().split("\n")
  13. data = open(fn).read().split("\n")
  14. terminal_commands = [x for x in data]
  15.  
  16. database_files = []
  17. set_filenames = set()
  18. file_path_list = []
  19. set_dirs = set()
  20. set_dirs_code_line = set()
  21.  
  22. # Definition to get dict which contains metadate for all files
  23. def get_temp_dict(name, size, file_path_list):
  24. return {
  25. "name": name,
  26. "size": size,
  27. "filepath": file_path_list,
  28. }
  29.  
  30.  
  31. for count, line in enumerate(terminal_commands):
  32. print(count)
  33. command = line[:4]
  34. if command == "dir ":
  35. set_dirs_code_line.add(line[4:])
  36. if command == "$ cd":
  37. target = line[5:]
  38. if target == "..":
  39. # We want to pop the list
  40. file_path_list.pop()
  41. else:
  42. file_path_list += [target]
  43. set_dirs.add(target)
  44. elif line[0] in ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]:
  45. name = line.split()[1]
  46. size = int(line.split()[0])
  47. temp_dict = copy.deepcopy(get_temp_dict(name, size, file_path_list))
  48. database_files += [temp_dict]
  49. set_filenames.add(name)
  50. else:
  51. "Nothing"
  52.  
  53. size_dir_dict = {}
  54.  
  55.  
  56. for dir in set_dirs:
  57. size_dir = [file["size"] for file in database_files if (dir in file["filepath"])]
  58. size_dir_dict[dir] = sum(size_dir)
  59. print("Len of size dir list")
  60. print(len(size_dir_dict))
  61.  
  62. print(sum([val for val in size_dir_dict.values() if val <= 100000]))
  63.  
  64.  
Advertisement
Add Comment
Please, Sign In to add comment