-
Notifications
You must be signed in to change notification settings - Fork 0
/
07.py
36 lines (29 loc) · 787 Bytes
/
07.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from lib import *
input = read_input(2022, 7)
sizes = {}
seen = set()
pwd = ()
for line in input.splitlines():
if match := re.match(r"^\$ cd (.+)$", line):
d = match[1]
if d == "/":
pwd = ()
elif d == "..":
pwd = pwd[:-1]
else:
pwd = (*pwd, d)
elif match := re.match(r"^(\d+) (.+)$", line):
s = int(match[1])
n = match[2]
if (pwd, n) in seen:
continue
seen.add((pwd, n))
d = pwd
while True:
sizes[d] = sizes.get(d, 0) + s
if not d:
break
d = d[:-1]
print(sum(s for s in sizes.values() if s <= 100000))
free = 70000000 - sizes[()]
print(min(s for s in sizes.values() if s >= 30000000 - free))