-
Notifications
You must be signed in to change notification settings - Fork 1
/
07_No_Space_Left_On_Device.py
64 lines (52 loc) · 1.55 KB
/
07_No_Space_Left_On_Device.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
from aoc import *
from collections import defaultdict
cmds = readlines()
sizes = defaultdict(int)
curpath = []
joinpath = lambda: '/'.join(curpath)
for cmd in cmds:
if cmd == '$ cd ..':
tmp = joinpath()
curpath.pop()
sizes[joinpath()] += sizes[tmp]
continue
if cmd.startswith('$ cd'):
curpath.append(cmd.split()[-1])
continue
size = allints(cmd)
if size:
sizes[joinpath()] += size[0]
continue
while curpath:
tmp = joinpath()
curpath.pop()
sizes[joinpath()] += sizes[tmp]
print('Star 1:', sum(x for x in sizes.values() if x <= 100000))
space_needed = 30000000 - (70000000 - sizes['/'])
print('Star 2:', next(x for x in sorted(sizes.values()) if x >= space_needed))
### Alternative solution with recursion ###
cmds = readlines()
sizes = dict()
def readdir(curpath, ip):
total = 0
while ip < len(cmds):
cmd = cmds[ip]
if cmd == '$ cd ..':
ip += 1
break
if cmd.startswith('$ cd'):
curpath.append(cmd.split()[-1])
subsize, ip = readdir(curpath, ip+1)
total += subsize
curpath.pop()
continue
size = allints(cmd)
if size:
total += size[0]
ip += 1
sizes['/'.join(curpath)] = total
return total, ip
readdir([], 0)
print('Star 1 (recursion):', sum(x for x in sizes.values() if x <= 100000))
space_needed = 30000000 - (70000000 - sizes['/'])
print('Star 2 (recursion):', next(x for x in sorted(sizes.values()) if x >= space_needed))