This commit is contained in:
Sebastian Seedorf
2022-12-08 13:34:47 +01:00
parent b61471f365
commit 1bd0017a32
3 changed files with 1109 additions and 0 deletions

1045
day07/input.txt Normal file

File diff suppressed because it is too large Load Diff

31
day07/part1.py Normal file
View File

@@ -0,0 +1,31 @@
#!/usr/bin/env python3
from typing import Generator, Any
lines = (x.strip() for x in open("input.txt"))
root = dict()
sizes = []
def parse(obj: dict, gen: Generator[str, Any, None]):
for line in gen:
if line == '$ ls' or line == "$ cd /":
continue
elif line == "$ cd ..":
return obj
elif line[:4] == "$ cd":
parse(obj[line[5:]], gen)
elif line[0] == "d":
obj[line[4:]] = dict()
else:
size, name = line.split(" ", 1)
obj[name] = int(size)
return obj
def calc(obj: dict, sizes: list):
s = sum(v if isinstance(v, int) else calc(v, sizes) for v in obj.values())
sizes.append(s)
return s
parse(root, lines)
calc(root, sizes)
print(sum(size for size in sizes if size <= 100000))

33
day07/part2.py Normal file
View File

@@ -0,0 +1,33 @@
#!/usr/bin/env python3
from typing import Generator, Any
lines = (x.strip() for x in open("input.txt"))
root = dict()
sizes = []
def parse(obj: dict, gen: Generator[str, Any, None]):
for line in gen:
if line == '$ ls' or line == "$ cd /":
continue
elif line == "$ cd ..":
return obj
elif line[:4] == "$ cd":
parse(obj[line[5:]], gen)
elif line[0] == "d":
obj[line[4:]] = dict()
else:
size, name = line.split(" ", 1)
obj[name] = int(size)
return obj
def calc(obj: dict, sizes: list):
s = sum(v if isinstance(v, int) else calc(v, sizes) for v in obj.values())
sizes.append(s)
return s
parse(root, lines)
root_size = calc(root, sizes)
free_up = root_size - 40000000
print(next(i for i in sorted(sizes) if i > free_up))