diff --git a/README.md b/README.md index 8e71791..9fcefee 100644 --- a/README.md +++ b/README.md @@ -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) | | | | diff --git a/input/22/test b/input/22/test deleted file mode 100644 index 66ae161..0000000 --- a/input/22/test +++ /dev/null @@ -1,23 +0,0 @@ - -2 root -3 root -4 root -5 root -6 root -7 a -8 a -9 a -10 a -11 a -12 a -13 e -14 e -15 e -16 .. -17 .. -18 d -19 d -20 d -21 d -22 d -23 d \ No newline at end of file diff --git a/py/22/day07.py b/py/22/day07.py index fe7a68e..a07d56f 100644 --- a/py/22/day07.py +++ b/py/22/day07.py @@ -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 \ No newline at end of file + 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)