-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e54d271
commit d22e71b
Showing
3 changed files
with
22 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,36 @@ | ||
from collections import deque | ||
from collections import defaultdict | ||
from pathlib import Path | ||
|
||
|
||
def start_day(): | ||
type_data = 'str' | ||
return type_data | ||
|
||
|
||
def helper(data): | ||
current_dir_letter = 'root' # directory as default | ||
dir_letter = [] # a, d, e | ||
temp_dir_letter = [] # a, e -> a -> d | ||
dirs = {} | ||
|
||
_data = data | ||
|
||
if data[0] == '$ cd /': | ||
_data = data[1:] | ||
|
||
for index, value in enumerate(_data): | ||
try: | ||
first, second, current_dir_letter = value.split()[0], value.split()[1], value.split()[2] | ||
except IndexError: | ||
first, second = value.split()[0], value.split()[1] | ||
|
||
if first == '$' and second == 'ls': | ||
continue | ||
|
||
if first == 'dir' and second.isalpha(): | ||
dir_letter.append(second) | ||
continue | ||
|
||
if first == '$' and second == 'cd' and current_dir_letter.isalpha(): | ||
temp_dir_letter.append(current_dir_letter) | ||
continue | ||
|
||
if first.isnumeric() and current_dir_letter in temp_dir_letter: | ||
if len(temp_dir_letter) > 1: | ||
first_letter = temp_dir_letter[:1][0] | ||
dirs.setdefault(first_letter, []).append(first) | ||
else: | ||
dirs.setdefault(current_dir_letter, []).append(first) | ||
continue | ||
|
||
if first == '$' and second == 'cd' and current_dir_letter == '..': | ||
temp_dir_letter.remove(str(temp_dir_letter[-1])) | ||
continue | ||
|
||
for letter, size in dirs.items(): | ||
total_size = sum(int(item) for item in size) | ||
dirs[letter] = total_size | ||
def helper(_data): | ||
curr_dir = Path('/') | ||
dirs = defaultdict(int) | ||
|
||
for line in _data: | ||
match line.split(): | ||
case ['$', 'cd', new_dir]: | ||
curr_dir = curr_dir / new_dir | ||
curr_dir = curr_dir.resolve() | ||
case [size, _] if size.isdigit(): | ||
size = int(size) | ||
for path in [curr_dir, *curr_dir.parents]: | ||
dirs[path] += size | ||
return dirs | ||
|
||
|
||
def part_1(data): | ||
maximum_size = 100000 | ||
dirs = helper(data) | ||
|
||
return sum(x for x in dirs.values() if x <= maximum_size) | ||
|
||
return sum(size for size in helper(data).values() if size <= maximum_size) | ||
|
||
|
||
def part_2(data): | ||
message = helper(data) | ||
return message | ||
available_space = 70000000 | ||
required_space = 30000000 | ||
dirs = helper(data) | ||
|
||
return min(size for size in dirs.values() if dirs[Path('/')] - size <= available_space - required_space) |