Skip to content

Commit

Permalink
day07 refactor part_1 + part_2
Browse files Browse the repository at this point in the history
  • Loading branch information
Vasile-Hij committed May 20, 2023
1 parent e54d271 commit d22e71b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 74 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ For testing sample input add `-s s`: `python main.py -v 01 s- s`
| [04](https://adventofcode.com/2022/day/4) | Camp Cleanup | [py](py/22/day04.py) | 433 | 852 | ** |
| [05](https://adventofcode.com/2022/day/5) | Supply Stacks | [py](py/22/day05.py) | JRVNHHCSJ | GNFBSBJLH | ** |
| [06](https://adventofcode.com/2022/day/6) | Turning Trouble | [py](py/22/day06.py) | 1287 | 3716 | ** |
| [07](https://adventofcode.com/2022/day/7) | No Space Left On Device | [py](py/22/day07.py) | 1367870 | | * |
| [07](https://adventofcode.com/2022/day/7) | No Space Left On Device | [py](py/22/day07.py) | 1367870 | 549173 | ** |
| [08](https://adventofcode.com/2022/day/8) | | [py](py/22/day08.py) | | | |
| [09](https://adventofcode.com/2022/day/9) | | [py](py/22/day09.py) | | | |
| [10](https://adventofcode.com/2022/day/10) | | [py](py/22/day10.py) | | | |
Expand Down
23 changes: 0 additions & 23 deletions input/22/test

This file was deleted.

71 changes: 21 additions & 50 deletions py/22/day07.py
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)

0 comments on commit d22e71b

Please sign in to comment.