day 7 python solution

main
magical 2022-12-06 21:20:05 -08:00
parent 9848440f3f
commit d2d54b8a6e
3 changed files with 1079 additions and 0 deletions

1014
day07/input 100644

File diff suppressed because it is too large Load Diff

23
day07/sample 100644
View File

@ -0,0 +1,23 @@
$ cd /
$ ls
dir a
14848514 b.txt
8504156 c.dat
dir d
$ cd a
$ ls
dir e
29116 f
2557 g
62596 h.lst
$ cd e
$ ls
584 i
$ cd ..
$ cd ..
$ cd d
$ ls
4060174 j
8033020 d.log
5626152 d.ext
7214296 k

42
day07/sol.py 100644
View File

@ -0,0 +1,42 @@
import sys
from collections import defaultdict
cmds = open("input").read().split("$")
cwd = []
sizes = defaultdict(lambda: 0)
for cmd in cmds:
cmd = cmd.strip()
if not cmd:
continue
print(cmd)
if cmd.startswith("cd "):
dir = cmd.split()[1]
if dir == "..":
cwd.pop()
elif dir == "/":
cwd = []
else:
cwd.append(dir)
elif cmd.startswith("ls\n"):
files = cmd.splitlines()[1:]
for file in files:
sz, name = file.split()
if sz == 'dir':
pass
else:
print(cwd, name, sizes)
for i in range(len(cwd)+1):
sizes['/'.join(cwd[:i])] += int(sz)
else:
print("bad command:" +cmd)
print(sizes)
t = 0
for sz in sizes.values():
if sz <= 100000:
t += sz
print(t)
print(min(sz for sz in sizes.values() if 70000000 - sizes[''] + sz >= 30000000))