Skip to content

Commit de559e2

Browse files
author
Florian Rohrer
committed
Day 7
1 parent 6c2f074 commit de559e2

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
|[Day 4: Camp Cleanup](https://adventofcode.com/2022/day/4) |[py](/day04/main.py)|
99
|[Day 5: Supply Stacks](https://adventofcode.com/2022/day/5) |[py](/day05/main.py)|
1010
|[Day 6: Tuning Trouble](https://adventofcode.com/2022/day/6) |[py](/day06/main.py), [alt](/day06/alt.py)|
11+
|[Day 7: No Space Left On Device](https://adventofcode.com/2022/day/7) |[py](/day07/main.py)|
1112

1213
My solutions from previous years:
1314
* [r0f1/adventofcode2020](https://github.com/r0f1/adventofcode2020)

day07/main.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
with open("input.txt") as f:
2+
lines = [x.strip().split() for x in f]
3+
4+
root = {".name": "/", ".type": "d"}
5+
cwd = None
6+
path = []
7+
for t in lines:
8+
if t[0] == "$":
9+
if t[1] == "cd":
10+
if t[2] == "/": cwd = root
11+
elif t[2] == "..": cwd = path.pop()
12+
else: path.append(cwd); cwd = cwd[t[2]]
13+
elif t[0] == "dir": cwd[t[1]] = {".name": t[1], ".type": "d"}
14+
else: cwd[t[1]] = {".name": t[1], ".type": "f", ".size": int(t[0])}
15+
16+
def get_size(n):
17+
if n[".type"] == "f": return n[".size"]
18+
return sum(get_size(v) for k, v in n.items() if not k.startswith("."))
19+
20+
def create_size_list(cwd, all_sizes):
21+
all_sizes.append(get_size(cwd))
22+
for k, v in cwd.items():
23+
if not k.startswith(".") and v[".type"] == "d":
24+
create_size_list(v, all_sizes)
25+
26+
all_sizes = []
27+
create_size_list(root, all_sizes)
28+
print(sum([s for s in all_sizes if s < 100_000]))
29+
30+
size_free = 70_000_000 - all_sizes[0]
31+
for d in sorted(all_sizes):
32+
if size_free + d >= 30_000_000:
33+
print(d)
34+
break

0 commit comments

Comments
 (0)