Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- from collections import defaultdict
- import copy
- from functools import lru_cache
- from string import ascii_lowercase, ascii_uppercase
- import os
- import numpy as np
- os.chdir("/Users/Me/Desktop/AdventOfCode/07/")
- fn = "example.txt"
- # fn = "input.txt"
- # data = open(fn).read().strip().split("\n")
- data = open(fn).read().split("\n")
- terminal_commands = [x for x in data]
- database_files = []
- set_filenames = set()
- file_path_list = []
- set_dirs = set()
- set_dirs_code_line = set()
- # Definition to get dict which contains metadate for all files
- def get_temp_dict(name, size, file_path_list):
- return {
- "name": name,
- "size": size,
- "filepath": file_path_list,
- }
- for count, line in enumerate(terminal_commands):
- print(count)
- command = line[:4]
- if command == "dir ":
- set_dirs_code_line.add(line[4:])
- if command == "$ cd":
- target = line[5:]
- if target == "..":
- # We want to pop the list
- file_path_list.pop()
- else:
- file_path_list += [target]
- set_dirs.add(target)
- elif line[0] in ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]:
- name = line.split()[1]
- size = int(line.split()[0])
- temp_dict = copy.deepcopy(get_temp_dict(name, size, file_path_list))
- database_files += [temp_dict]
- set_filenames.add(name)
- else:
- "Nothing"
- size_dir_dict = {}
- for dir in set_dirs:
- size_dir = [file["size"] for file in database_files if (dir in file["filepath"])]
- size_dir_dict[dir] = sum(size_dir)
- print("Len of size dir list")
- print(len(size_dir_dict))
- print(sum([val for val in size_dir_dict.values() if val <= 100000]))
Advertisement
Add Comment
Please, Sign In to add comment