So, this one was the first one so far that was tricky. Even though it’s not really that hard, I had actually already planned it out for the most part from just reading the prompt. You essentially get a series of Command Line commands and files with sizes, and have to move around a file system adding up numbers (file sizes).

I built a dictionary to track the total file size for the directories, and a list to keep track of where I was in the directory tree. Easy easy so far. Also, everything worked will on the sample data.

But I got stuck, because my totals were not matching. I tried like a dozen different slight modification to the method, and manually parsed through chunks of print statement results. Everything SEEMED to be in order here. Eventually I even went to Reddit to find someone’s completed project to see what number I was shooting for, because sometimes I’m off by a straight forward amount, and the solution becomes blatantly obvious.

This was not the case, BUT the Part 2 solution I got from the other person’s code, was in my results. I had no idea what Part 2 was, but clearly I was getting good data, because the answer was already present in my data.

Eventually I used the other person’s solution to unlock Part 2, which I solved pretty quickly, which led me to believe even more that my process for part 1 was good.

I decided to make one additional modification to my code, in how it handled keys for the directory dictionary. Instead of “Directory: Size” I modified it so it was “RootDirectoryDirectoryDirectory(etc): size.

And it worked.

I was getting the right answer.

I suspect, the issue is that the directory names repeat within the folder structure, which means that when it was supposed to add to “Directory/Directory/Directory” it was instead adding to some previously established “OtherDirectory/OtherDirectory/Directory”.

Confused yet?

Anyway, here is my code, and it works, for both solutions.

with open("Day07Input.txt") as file:
    data = file.read()

split = data.split('\n')
tree_dict = {"/": 0}
current_dir = []

for each in split:
    cl = each.split(" ")
    if cl[0] == "$":
        if cl[1] == "cd":
            if cl[2] == "/":
                current_dir = ["/"]
            elif cl[2] == "..":
                current_dir.pop()
            else:
                current_dir.append(cl[2])
    elif cl[0] == "dir":
        tree_dict["".join(current_dir)+cl[1]] = 0
    else:
        for tree_walker in range(0,len(current_dir)):
            tree_dict["".join(current_dir[0:tree_walker+1])] += int(cl[0])
    # print(current_dir)
    # print(files)

# print(tree_dict)

space_needed = 30000000 - (70000000-tree_dict['/'])
size_to_delete = tree_dict["/"]
total_size = 0
for key in tree_dict.keys():
    if tree_dict[key] <= 100000:
        total_size += tree_dict[key]
    if tree_dict[key] > space_needed and tree_dict[key] < size_to_delete:
        size_to_delete = tree_dict[key]

print(total_size)
# 6199378 high
# 1048940 low
# 1145908 low

print(size_to_delete)

##Part 1: 1582412
##Part 2: 3696336