-
Notifications
You must be signed in to change notification settings - Fork 0
/
09_DiskFragmenter.py
80 lines (65 loc) · 1.83 KB
/
09_DiskFragmenter.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import sys, collections, re, itertools, functools, math
from lib import *
inp = get_input()[0]
File = collections.namedtuple("File", ["size", "id"])
R: list[File] = []
file_id = 0
for i,c in enumerate(inp):
if i%2==0:
R.append(File(int(c), file_id))
file_id += 1
else:
R.append(File(int(c), "."))
# helper to print files list
def ff(R: list[File]):
return "".join(str(r.id)*r.size for r in R)
def p1(R: list[File]):
# defragment files into smaller files with size=1
R = [File(1, r.id) for r in R for _ in range(r.size)]
l = 0
r = len(R) - 1
while l <= r:
if R[l].id != ".":
l += 1
continue
if R[r].id == ".":
r -= 1
continue
R[l], R[r] = R[r], R[l]
r-=1
l+=1
return R
def p2(R: list[File]):
curr_file = file_id - 1 # start with last file (calc max used file id)
r = len(R) - 1
while curr_file > 0:
if R[r].id == curr_file:
l = 0
while l <= r:
if R[l].id == "." and R[l].size >= R[r].size:
R.insert(l, R[r])
R[l+1] = File(R[l+1].size - R[r+1].size, R[l+1].id)
R[r+1] = File(R[r+1].size, ".")
if R[l+1].size == 0:
del R[l+1]
curr_file -= 1
r = len(R)-1 # reset right pointer
break
else:
l += 1
else:
curr_file -= 1
else:
r -= 1
return R
def checksum(R: list[File]):
res = 0
idx = 0
for f in R:
for _ in range(f.size):
if f.id != ".":
res += idx * f.id
idx += 1
return res
print(checksum(p1(R[:])))
print(checksum(p2(R[:])))