-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
25 lines (23 loc) · 893 Bytes
/
main.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
import re
with open('input.txt') as file:
lines = file.read().splitlines()[1:]
dirs, pwd = {}, '/'
for line in lines:
if line[0] == '$':
if line[2] == 'c':
if line[5] == '.' and line[6] == '.':
pwd = re.sub(r'[^/]+?/$', '', pwd, 1)
else:
cd = line.split('$ cd ')[1] + '/'
pwd = f'{pwd}{cd}'
elif line[0].isnumeric():
temp_pwd = pwd
file_size = int(re.findall(r'^([0-9]+)', line).pop())
for _ in range(pwd.count('/')):
try:
dirs[temp_pwd] = dirs[temp_pwd] + file_size
except KeyError:
dirs[temp_pwd] = file_size
temp_pwd = re.sub(r'[^/]+?/$', '', temp_pwd, 1)
print(sum([(i if i <= 100000 else 0) for i in dirs.values()]))
print(min(i for i in dirs.values() if i > ((70000000 - dirs['/'] - 30000000) * -1)))