-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday7.py
49 lines (36 loc) · 1.43 KB
/
day7.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
from aocd.models import Puzzle
from collections import defaultdict
"""
Remember to add AOC_SESSION as an environment variable. It is a cookie from AOC website called session.
"""
def get_puzzle():
puzzle = Puzzle(year=2022, day=7)
data = puzzle.input_data
data = [line for line in data.split("\n")]
data = [line.strip("\n").rsplit(' ', 1) for line in data]
return data
def directory_info(entry):
return (True, entry[1]) if entry[0] == "$ cd" else (False, int(entry[0])) if entry[0][0].isdigit() else None
def dir_collection():
current_path = list()
dir_tree = defaultdict(lambda: 0)
directory_list = [directory_info(i) for i in get_puzzle() if directory_info(i) is not None]
for dir_change, info in directory_list:
if dir_change:
current_path.pop() if info == ".." else current_path.append(info)
else:
for i in range(len(current_path)):
dir_tree["".join(current_path[0:i+1])] += info
return dir_tree
def part_1():
max_size = 100000
dir_tree = dir_collection()
return sum(i for i in dir_tree.values() if i <= max_size)
def part_2():
total_space = 70000000
required_space = 30000000
dir_tree = dir_collection()
space_to_del = required_space - (total_space - dir_tree["/"])
return min(i for i in dir_tree.values() if i - space_to_del >= 0)
print(f"Answer for part 1 is {part_1()}")
print(f"Answer for part 2 is {part_2()}")