Part 1 | Part 2 | Total | |
---|---|---|---|
Time | 38:28 | 4:30 | 42:58 |
Oh. My. Goodness. I had it in 10 minutes! The structure of this code was entirely unchanged since 10 minutes in. However, instead of storing the path
in the dictionary, I stored only the bottom-most folder. This of course did NOT work when there were multiple folders of the same name. I spent the next 20+ minutes trying to figure out why the numbers didn't work until I looked in the input and saw $ cd ngmqbc
shortly followed by another $ cd ngmqbc
, then it clicked. Very frustrating error, but a whole lot of fun and clever problem!
from helpers.datagetter import data_in, submit
from collections import defaultdict
din = data_in(split=True, numbers=False)
ans = 0
dirs = defaultdict(int)
current = []
for line in din:
if line.startswith("$ cd"):
dir = line.split(" ")[2]
if dir == "..":
current.pop()
elif dir == "/":
current = ["/"]
else:
current.append(dir)
elif line.split(" ")[0].isnumeric():
for i in range(len(current)):
path = "/".join(current[0:i+1])
dirs[path] += int(line.split(" ")[0])
for thing in dirs.values():
### SECTION TO CHANGE FOR PART 2
if thing <= 100000:
ans += thing
### END SECTION
print(ans)
submit(ans)
Very small modification to look for the smallest folder that can be subtracted to give a used size of less than 40,000,000.
if dirs["/"] - thing <= 40000000:
ans = min(thing, ans)