Skip to content

Commit 4a8bb1e

Browse files
committed
Day 7
1 parent de559e2 commit 4a8bb1e

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +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)|
11+
|[Day 7: No Space Left On Device](https://adventofcode.com/2022/day/7) |[py](/day07/main.py), [alt](/day07/alt.py)|
1212

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

day07/alt.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# hugues_hoppe on Reddit
2+
from itertools import accumulate
3+
4+
with open("input.txt") as f:
5+
lines = [x.strip() for x in f]
6+
7+
stack = []
8+
sizes = []
9+
for line in lines:
10+
t = line.split()
11+
if line == "$ cd ..":
12+
s = stack.pop()
13+
sizes.append(s)
14+
stack[-1] += s
15+
elif line.startswith("$ cd "):
16+
stack.append(0)
17+
elif t[0].isdigit():
18+
stack[-1] += int(t[0])
19+
sizes.extend(accumulate(stack[::-1]))
20+
21+
print(sum(s for s in sizes if s <= 100_000))
22+
print(min(s for s in sizes if s >= sizes[-1] - 40_000_000))

day07/main.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
with open("input.txt") as f:
22
lines = [x.strip().split() for x in f]
33

4-
root = {".name": "/", ".type": "d"}
4+
root = {".type": "d"}
55
cwd = None
66
path = []
77
for t in lines:
@@ -10,8 +10,8 @@
1010
if t[2] == "/": cwd = root
1111
elif t[2] == "..": cwd = path.pop()
1212
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])}
13+
elif t[0] == "dir": cwd[t[1]] = {".type": "d"}
14+
else: cwd[t[1]] = {".type": "f", ".size": int(t[0])}
1515

1616
def get_size(n):
1717
if n[".type"] == "f": return n[".size"]
@@ -25,7 +25,7 @@ def create_size_list(cwd, all_sizes):
2525

2626
all_sizes = []
2727
create_size_list(root, all_sizes)
28-
print(sum([s for s in all_sizes if s < 100_000]))
28+
print(sum(s for s in all_sizes if s < 100_000))
2929

3030
size_free = 70_000_000 - all_sizes[0]
3131
for d in sorted(all_sizes):

0 commit comments

Comments
 (0)