-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9_2.py
57 lines (48 loc) · 1.64 KB
/
9_2.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
def main():
with open('input/9_2.txt') as f:
contents = ''.join(f.read().splitlines())
layout = parse_layout(contents)
defragment_disk(layout)
disk_layout = []
for char, size in layout:
for _ in range(size):
disk_layout.append(char)
total = 0
for idx, char in enumerate(disk_layout):
if char == '.':
continue
total += int(char) * idx
print(total)
def parse_layout(contents):
layout = []
for idx, char in enumerate(contents):
if idx % 2 == 0:
layout.append((str(idx // 2), int(char)))
else:
layout.append(('.', int(char)))
return layout
def defragment_disk(layout):
rev_idx = len(layout) - 1
while rev_idx >= 0:
file = layout[rev_idx]
if file[0] == '.':
rev_idx -= 1
continue
for space_idx, space in enumerate(layout):
if space[0] != '.':
continue
if rev_idx > space_idx and space[1] >= file[1]:
file_idx = layout.index(file)
layout[space_idx] = (file[0], file[1])
if space[1] == file[1]:
# if the space is the same size as the file, just swap them
layout[file_idx] = ('.', file[1])
break
else:
# if the space is bigger than the file, split the space and move the file
layout.insert(space_idx + 1, ('.', space[1] - file[1]))
layout[file_idx + 1] = ('.', file[1])
break
rev_idx -= 1
if __name__ == "__main__":
main()