-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday07.py
153 lines (122 loc) · 3.23 KB
/
day07.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
from dataclasses import dataclass
from typing import List
from typing import Optional
# input = """$ cd /
# $ ls
# dir a
# 14848514 b.txt
# 8504156 c.dat
# dir d
# $ cd a
# $ ls
# dir e
# 29116 f
# 2557 g
# 62596 h.lst
# $ cd e
# $ ls
# 584 i
# $ cd ..
# $ cd ..
# $ cd d
# $ ls
# 4060174 j
# 8033020 d.log
# 5626152 d.ext
# 7214296 k
# """.splitlines()
file = open("day07_input.txt", "r")
input = file.readlines()
file.close()
class File:
name: str
size: int
def __init__(self, name: str, size: int):
self.name = name
self.size = size
def __str__(self):
return self.name
@dataclass
class Dir:
name: str
father: Optional["Dir"]
files: List[File]
dirs: List["Dir"]
def __init__(self, name: str, father: Optional["Dir"]):
self.name = name
self.father = father
self.files = []
self.dirs = []
def __str__(self):
return self.name
@property
def size(self) -> int:
result = sum([file.size for file in self.files])
result += sum([dir.size for dir in self.dirs])
return result
def get_dir_by_name(
self,
name: str,
) -> "Dir":
return [dir for dir in self.dirs if dir.name == name][0]
def __ge__(self, other):
return self.size >= other.size
def __lt__(self, other):
return self.size <= other.size
def get_dirs_size_max(
root: Dir,
size_max: int = 100000,
result: Optional[List[Dir]] = None,
) -> List["Dir"]:
result = [] if not result else result
if root.size <= size_max:
result.append(root)
for dir in root.dirs:
result += get_dirs_size_max(root=dir, size_max=size_max)
return result
def get_dirs_size_min(
root: Dir,
size_min: int = 100000,
result: Optional[List[Dir]] = None,
) -> List["Dir"]:
result = [] if not result else result
if root.size >= size_min:
result.append(root)
for dir in root.dirs:
result += get_dirs_size_min(root=dir, size_min=size_min)
return result
root = Dir(name="/", father=None)
current_folder = root
for line in input:
line = line.split()
if line[0] == "$":
if line[1] == "ls" or line[2] == "/":
continue
if line[2] == "..":
current_folder = current_folder.father
else:
current_folder = current_folder.get_dir_by_name(name=line[2])
elif line[0] == "dir":
current_folder.dirs.append(Dir(name=line[1], father=current_folder))
else:
current_folder.files.append(File(size=int(line[0]), name=line[1]))
# assert root.size == 48381165
# assert root.get_dir_by_name(name="d").size == 24933642
# assert root.get_dir_by_name(name="a").size == 94853
# assert root.get_dir_by_name(name="a").get_dir_by_name("e").size == 584
# total_size = 0
# dirs = get_dirs_size_max(root, size_max=100000)
# for dir in dirs:
# total_size += dir.size
# assert total_size == 95437
# print(total_size)
TOTAL_DISK = 70000000
MINIMUM = 30000000
total_size = root.size
free = TOTAL_DISK - total_size
# assert free == 21618835
needed = MINIMUM - free
# assert needed == 8381165
dirs = get_dirs_size_min(root, size_min=needed)
# assert min(dirs).size == 24933642
print(min(dirs).size)