-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart1.py
64 lines (52 loc) · 1.91 KB
/
part1.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from collections import defaultdict
from typing import List, DefaultDict
def go_up_one_level(current_directory: str) -> str:
if current_directory == '/':
return '/'
return current_directory[:current_directory.rfind('/')]
def find_size_of_directory(paths: DefaultDict[str, List[str]], current_directory: str) -> int:
amount = 0
for file in paths[current_directory]:
if file.startswith('dir '):
name = file.replace('dir ', '')
amount += find_size_of_directory(paths, current_directory + '/' + name)
else:
amount += int(file.split(' ')[0])
return amount
def populate_paths(inp: List[str]) -> DefaultDict[str, List[str]]:
paths = defaultdict(list)
current_directory = ''
populating = False
for line in inp:
if line.startswith('$ '):
command = line.replace('$ ', '')
if command.startswith('cd'):
location = command.replace('cd ', '')
populating = False
if location == '/':
current_directory = '/'
elif location == '..':
current_directory = go_up_one_level(current_directory)
else:
current_directory += '/' + location
elif command.startswith('ls'):
populating = True
elif populating:
paths[current_directory].append(line)
return paths
def solution(inp: List[str]) -> int:
paths = populate_paths(inp)
answer = 0
limit = 100000
for path in paths:
size = find_size_of_directory(paths, path)
if size <= limit:
answer += size
return answer
def result(inp: List[str]) -> int:
return solution(inp)
def test(examples: List[List[str]]) -> None:
example = 0
exp = 95437
res = result(examples[example])
assert res == exp, f"example {example}: result was {res}, expected {exp}"