-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday7.py
More file actions
80 lines (68 loc) · 2.42 KB
/
day7.py
File metadata and controls
80 lines (68 loc) · 2.42 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import os
from pathlib import Path
from dataclasses import dataclass
the_day = 7
download_data_path = Path("/Users/relyea/Downloads/input.txt")
local_data_path = "/Users/relyea/code/advent_of_code_2022/input"+str(the_day)+".txt"
if download_data_path.exists():
download_data_path.rename(local_data_path)
# local_data_path = "/Users/relyea/code/advent_of_code_2022/input"+str(the_day)+"_test.txt"
with open(local_data_path) as input_file:
inpstring = input_file.readlines()
data = [
line.strip()
for line in inpstring
]
# data = open(local_data_path).read().strip().split("\n")
@dataclass
class Fileinfo:
files: dict
dirs: list
SEPARATOR='/'
all_files = dict()
all_files['.'] = Fileinfo(files=dict(), dirs=list())
current_dir = '.'
iline = 0
while iline < len(data)-1:
iline += 1
if data[iline] == '$ ls':
iline += 1
while '$' not in data[iline]:
if 'dir ' in data[iline]:
the_dir = current_dir + SEPARATOR + data[iline].split(" ")[1]
all_files[current_dir].dirs.append(the_dir)
if the_dir not in all_files:
all_files[the_dir] = Fileinfo(files=dict(), dirs=list())
else:
all_files[current_dir].files[data[iline].split(" ")[1]] = int(data[iline].split(" ")[0])
iline += 1
if iline >= len(data):
break
iline -= 1
elif 'cd ' in data[iline]:
if data[iline].split(" ")[2] == "..":
print('GO UP', data[iline], current_dir)
current_dir = SEPARATOR.join(current_dir.split(SEPARATOR)[:-1])
print('NOW', current_dir)
else:
print('GO DOWN', data[iline], current_dir)
current_dir += SEPARATOR + data[iline].split(" ")[2]
print('NOW', current_dir)
dir_sizes = dict()
sorted_dirs = sorted(all_files.keys(),key=len)[::-1]
for dir in sorted_dirs:
dir_sizes[dir] = 0
for file in all_files[dir].files:
dir_sizes[dir] += all_files[dir].files[file]
for subdir in all_files[dir].dirs:
dir_sizes[dir] += dir_sizes[subdir]
total_size_gt_100k = 0
for dir in dir_sizes:
if dir_sizes[dir] <= 100000:
total_size_gt_100k += dir_sizes[dir]
print(total_size_gt_100k)
space_to_free = dir_sizes['.']-40000000
max_delete = dir_sizes['.']
for dir in dir_sizes:
if dir_sizes[dir] < max_delete and dir_sizes[dir] > space_to_free:
max_delete = dir_sizes[dir]