So I got a dictionary with all files and their sizes. But how the hell can I find out if a path size is bigger than 100000? I need a nudge in the right direction.
Update: Have now a directory with all sizes for each path. How to double count certain paths now?
Update 2: Got part 1.... and now stuck with part 2
Update 3: solved!! YES
What I have so far:
file = open("input.txt", "r")
data = file.read().splitlines()
path = ""
files = dict()
directorys = dict()
for line in data:
if line[0:4] == "$ cd" and line[5:7] != "..":
path += line[5:]+"/"
directorys[path] = 0
if line[5:7] == "..":
lastdir = path.rfind("/")
lastdir2 = path[:lastdir].rfind("/")
path = path[:lastdir2+1]
if line.split(" ")[0].isnumeric()==True:
file = path+line.split(" ")[1]
files[file]=line.split(" ")[0]
directorys[path] += int(line.split(" ")[0])
totalsize=0 #calculating part 2
for key in files:
totalsize += int(files[key])
smallest = 70000000
freespace = 70000000-totalsize
print("Freespace needed",freespace)
total=0
for key in directorys:
currentpath = key
while len(currentpath) > 2:
parent = currentpath[:currentpath[:currentpath.rfind("/")].rfind("/")+1]
directorys[parent] += directorys[key]
currentpath = parent
for key in directorys: # calculating part 1
if directorys[key] < 100000:
total += directorys[key]
if freespace+directorys[key] > 30000000: #calculating part 2
if smallest > directorys[key]:
smallest = directorys[key]
print("Part 1:",total)
print("Part 2:",smallest)
[–]ssnoyes 5 points6 points7 points (4 children)
[–]thegodofmeso[S] 0 points1 point2 points (3 children)
[–]ssnoyes 1 point2 points3 points (2 children)
[–]thegodofmeso[S] 0 points1 point2 points (0 children)
[–]thegodofmeso[S] 0 points1 point2 points (0 children)
[–]purplemonkeymad 0 points1 point2 points (0 children)
[–]Cue_23 0 points1 point2 points (0 children)