Unable to load image

Advent of Code 2022: Day 7

https://adventofcode.com
with open('AdventOfCode2022/input.txt', 'r') as input:
    commands = input.read().split('\n')

    def new_dict(name, parent=None):
        return {
        'name': name,
        'parent':parent,
        'files': 0,
        'dirs': {}
    }

    root_dict = new_dict(name='/', parent=None)
    curr_dict = root_dict

    #Build tree of dicts
    for i in range(len(commands)):
        command_parts = commands[i].split(' ')
        if command_parts[0] == '$':
            if command_parts[1] == 'cd':
                if command_parts[2] == '..':
                    curr_dict = curr_dict['parent']
                elif command_parts[2] == '/':
                    pass
                else:
                    curr_dict = curr_dict['dirs'][command_parts[2]]
        elif command_parts[0] == 'dir':
            if command_parts[1] not in curr_dict:
                curr_dict['dirs'][command_parts[1]] = new_dict(name=command_parts[1], parent=curr_dict)
        elif command_parts[0] == '':
            pass
        else:
            curr_dict['files'] += int(command_parts[0])

    #PART ONE

    MAX_THRESHOLD = 100000
    FINAL_COUNTER = 0
    dict_list = []
    
    def parse_dict(t_dict):
        folder_total = t_dict['files']

        for c_dict in t_dict['dirs'].keys():
            folder_total += parse_dict(t_dict['dirs'][c_dict])

        global dict_list
        dict_list.append(folder_total)

        if folder_total < MAX_THRESHOLD:
            global FINAL_COUNTER # this is the most embarrassing thing I've ever done
            FINAL_COUNTER += folder_total
        return folder_total

    total_used = parse_dict(root_dict)
    print(FINAL_COUNTER)

    #PART TWO
    TOTAL_SPACE = 70000000
    WANTED_SPACE = 30000000
    curr_wanted = WANTED_SPACE - (TOTAL_SPACE - total_used)
    
    larger_than_wanted = []
    for i in dict_list:
        if i > curr_wanted:
            larger_than_wanted.append(i)

    print(min(larger_than_wanted))
32
Jump in the discussion.

No email address required.

wtf they added a timeout if you enter wrong things? and i wasnt even spamming stuff

![](/images/16704022161217477.webp)

Jump in the discussion.

No email address required.

i don't understand how people have this happen, just run your code against the dummy input they give you and verify you get the right output

Jump in the discussion.

No email address required.

Not necessarily, I'm getting the right answer with their testinput and the wrong answer on the real one :marseyxd:

Jump in the discussion.

No email address required.

how tho

Jump in the discussion.

No email address required.

Not sure yet. I'm fairly certain I must be miscounting something as the file paths get deeper but haven't had time to hunt down exactly what I did wrong yet.

fileDict = dict()

lsFound = False

with open('input', 'r') as f:

    location = ['.']

    for line in f:

        line = line.strip()

        if (line[0] is '$'):

            lsFound = False

        if lsFound and line[:3] not in 'dir':

            file_path = '/'.join(location)

            if (file_path in fileDict.keys()):

                if line not in fileDict.get(file_path):

                    fileDict.get(file_path).add(line)

                else:

                    print('Line already present')

            else:

                fileDict[file_path] = set()

                fileDict[file_path].add(line)

        if not lsFound:

            command = []

            if line[0] is '$':

                command = line[1:].strip().split(' ')

                if 'dir' in command[0]:

                    continue

                if 'cd' in command[0]:

                    if command[1] in '/':

                        location = ['.']

                    elif command[1] in '..':

                        location.pop()

                    else:

                        if (command[1] not in '/'):

                            location.append(command[1])

                elif 'ls' in command[0]:

                    lsFound = True

def computeSize(nameKey) -> int:

    tempSum = 0

    files = fileDict[nameKey]

    for size_name in files:

        pair = size_name.split(' ')

        if pair[0] in 'dir':

            continue

        tempSum += int(pair[0].strip())

    for key2 in fileDict.keys():

        if nameKey + '/' in key2 and nameKey is not key2:

            tempSum += computeSize(key2)

    return tempSum

totalSum = 0

print(fileDict)

for key in fileDict.keys():

    dirSum = computeSize(key)

    if dirSum <= 100000:

        totalSum += dirSum

print(totalSum)
Jump in the discussion.

No email address required.

Not 100% sure, but I think you are forgetting that the size of a directory is the size of the files in it and all the files in directories beneath it. So, its recursive

Jump in the discussion.

No email address required.

That was a mistake. You're about to find out the hard way why.

Jump in the discussion.

No email address required.

:#marseyastronaut::!#marseygun::#marseyastronaut2:

Also you were spamming stuff lol.

Jump in the discussion.

No email address required.

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.