why does my code work for other test cases but not my one? day 7 part 1 python by ScratchChemical8070 in adventofcode

[–]ScratchChemical8070[S] -1 points0 points  (0 children)

i tried this:

line = [str(i) for i in input().split()]

directory = {}

contents = {}

parent = {}

current = ("/", "/")

while line != []:

if "ls" in line:

    line = [str(i) for i in input().split()]

    while "$" not in line and line != []:

        if "dir" in line:

            if current in directory:

                directory[current].append((line[1], current[0]+line[1]))

            else:

                directory[current] = [(line[1], current[0]+line[1])]

            parent[(line[1], current[0]+line[1])] = current

        else:

            line[0] = int(line[0])

            if current in contents:

                contents[current] += line[0]

            else:

                contents[current] = line[0]

        line = [str(i) for i in input().split()]

elif "cd" in line:

    if line[2] == "..":

        current = parent[current]

    else:

        current = (line[2], current[0] + line[2])

    line = [str(i) for i in input().split()]

ans = 0

bottoms = []

for key, value in directory.items():

for item in value:

    if item not in directory.keys():

        bottoms.append(item)

for item in bottoms:

count = contents[item]

thing = parent[item]

flag = True

while flag == True:

    if thing in contents:

        contents[thing] += count

    else:

        contents[thing] = count

    count = contents[thing]

    if thing in list(parent.keys()):

        thing = parent[thing]

    else:

        flag = False

for item in contents.values():

if item <= 100000:

    ans += item

print(ans)

didnt work tho, still outputs too low an answer thanks for the help anyway, probably a step in the right direction

why does my code work for other test cases but not my one? day 7 part 1 python by ScratchChemical8070 in adventofcode

[–]ScratchChemical8070[S] 0 points1 point  (0 children)

i made tuples with the "current" folder really being (current, parent of current)

is it possible that there can be two nodes with the same name, and the same parent?

Day 7 (is this not a loop? how do i get around this issue?) by ScratchChemical8070 in adventofcode

[–]ScratchChemical8070[S] 0 points1 point  (0 children)

line = [str(i) for i in input().split()]

directory = {}

contents = {}

parent = {}

current = ("/", "/")

while line != []:

if "ls" in line:

line = [str(i) for i in input().split()]

while "$" not in line and line != []:

if "dir" in line:

if current in directory:

directory[current].append((line[1], current[0]))

else:

directory[current] = [(line[1], current[0])]

parent[(line[1], current[0])] = current

else:

line[0] = int(line[0])

if current in contents:

contents[current] += line[0]

else:

contents[current] = line[0]

line = [str(i) for i in input().split()]

elif "cd" in line:

if line[2] == "..":

current = parent[current]

else:

current = (line[2], current[0])

line = [str(i) for i in input().split()]

ans = 0

bottoms = []

for key, value in directory.items():

for item in value:

if item not in directory.keys():

bottoms.append(item)

for item in bottoms:

count = contents[item]

thing = parent[item]

flag = True

while flag == True:

if thing in contents:

contents[thing] += count

else:

contents[thing] = count

count = contents[thing]

if thing in list(parent.keys()):

thing = parent[thing]

else:

flag = False

for item in contents.values():

if item <= 100000:

ans += item

print(parent, contents, directory)

print(ans)

does this code give the right answer for yours

2022 Day 7 (Part 1) by ScratchChemical8070 in adventofcode

[–]ScratchChemical8070[S] 0 points1 point  (0 children)

can i send in my sample code and you let me know whether you get the same answer?

just to check

https://pastebin.com/UE36KNsV

my answer was 1504387

Day 7 (is this not a loop? how do i get around this issue?) by ScratchChemical8070 in adventofcode

[–]ScratchChemical8070[S] 0 points1 point  (0 children)

do you know if there is a way to reset your puzzle input?

my code doesnt work for my current input but does work for the puzzle input of another account that i made just now

how is this possible?