Skip to content

Commit 9631b92

Browse files
committed
day07: add solutions
1 parent 9a27564 commit 9631b92

File tree

4 files changed

+1159
-0
lines changed

4 files changed

+1159
-0
lines changed

day07/1.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env pypy3
2+
3+
import os.path
4+
import re
5+
import sys
6+
7+
SIZE_BOUND = 100000
8+
9+
def main():
10+
dirsizes = {}
11+
cwd = '/'
12+
for line in sys.stdin:
13+
if m := re.match(r'^\$ cd \.\.$', line):
14+
cwd, _ = os.path.split(cwd)
15+
elif m := re.match(r'^\$ cd (\S+)$', line):
16+
cwd = os.path.join(cwd, m.group(1))
17+
dirsizes[cwd] = 0
18+
elif m := re.match(r'^([0-9]+) (\S+)$', line):
19+
size = int(m.group(1))
20+
for d in filter(cwd.startswith, dirsizes):
21+
dirsizes[d] += size
22+
23+
print(sum(v for v in dirsizes.values() if v <= SIZE_BOUND))
24+
25+
if __name__ == '__main__':
26+
main()

day07/2.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env pypy3
2+
3+
import os.path
4+
import re
5+
import sys
6+
7+
SIZE_DISK = 70000000
8+
SIZE_NEED = 30000000
9+
10+
def main():
11+
dirsizes = {}
12+
cwd = '/'
13+
for line in sys.stdin:
14+
if m := re.match(r'^\$ cd \.\.$', line):
15+
cwd, _ = os.path.split(cwd)
16+
elif m := re.match(r'^\$ cd (\S+)$', line):
17+
cwd = os.path.join(cwd, m.group(1))
18+
dirsizes[cwd] = 0
19+
elif m := re.match(r'^([0-9]+) (\S+)$', line):
20+
size = int(m.group(1))
21+
for d in filter(cwd.startswith, dirsizes):
22+
dirsizes[d] += size
23+
24+
size_to_delete = SIZE_NEED - (SIZE_DISK - dirsizes['/'])
25+
print(min(v for v in dirsizes.values() if v >= size_to_delete))
26+
27+
if __name__ == '__main__':
28+
main()

0 commit comments

Comments
 (0)