-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolution.py
195 lines (153 loc) · 5.89 KB
/
solution.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import os
import time
import heapq
def clear_console():
os.system('cls' if os.name == 'nt' else 'clear')
def parse_input(filename):
with open(filename, 'r') as f:
return f.read().strip()
def split_disk_layout(input_str):
input_str = input_str.strip()
return [int(x) for x in input_str[::2]], [int(x) for x in input_str[1::2]]
def calc_disk_checksum(disk):
return sum(pos * fid for pos, fid in enumerate(disk) if fid != '.')
def build_initial_disk(files, spaces):
disk = []
for fid, size in enumerate(files):
disk.extend([fid] * size)
if fid < len(spaces):
disk.extend(['.'] * spaces[fid])
total_len = sum(files) + sum(spaces)
disk.extend(['.'] * (total_len - len(disk)))
return disk
def move_files_left(disk):
n = len(disk)
empty_pos = [i for i in range(n) if disk[i] == '.']
if not empty_pos:
return disk
files_moved = 0
total_files = sum(1 for x in disk if x != '.')
for i in range(n-1, -1, -1):
if disk[i] != '.':
if empty_pos and empty_pos[0] < i:
disk[empty_pos[0]] = disk[i]
disk[i] = '.'
empty_pos.pop(0)
empty_pos.append(i)
files_moved += 1
if files_moved % 1000 == 0:
clear_console()
print(f"Progress: {files_moved}/{total_files} files moved ({files_moved/total_files:.1%})")
return disk
def part_one(input_str):
files, spaces = split_disk_layout(input_str)
disk = build_initial_disk(files, spaces)
final = move_files_left(disk)
return calc_disk_checksum(final)
def part_one_optimized(input_str):
blocks = list(map(int, input_str[::2]))
spaces = list(map(int, input_str[1::2])) + [0]
disk = []
for i, (block, space) in enumerate(zip(blocks, spaces)):
disk.extend([i] * block)
disk.extend(['.'] * space)
empty = [i for i, val in enumerate(disk) if val == '.']
for i in range(len(disk)-1, -1, -1):
if disk[i] != '.' and empty and empty[0] < i:
disk[empty[0]] = disk[i]
disk[i] = '.'
empty.pop(0)
empty.append(i)
return calc_disk_checksum(disk)
def part_two_optimized(input_str):
blocks = list(map(int, input_str[::2]))
spaces = list(map(int, input_str[1::2])) + [0]
disk = [(pos, block, i) for i, (block, space) in enumerate(zip(blocks, spaces))
for pos in [sum(blocks[:i]) + sum(spaces[:i])] if block]
final = {}
space_left = dict(enumerate(spaces))
block_sums = [sum(blocks[:i]) for i in range(len(blocks) + 1)]
space_sums = [sum(spaces[:i]) for i in range(len(spaces) + 1)]
for pos, size, fid in reversed(disk):
moved = False
for i in range(fid):
if size <= space_left[i]:
offset = block_sums[i+1] + space_sums[i]
if space_left[i] != spaces[i]:
offset += spaces[i] - space_left[i]
space_left[i] -= size
if fid - 1 in space_left:
space_left[fid - 1] += size
final[fid] = (offset, size)
moved = True
break
if not moved:
final[fid] = (pos, size)
return sum(fid * (pos * size + size * (size - 1) // 2)
for fid, (pos, size) in final.items())
def part_two(input_str):
files = {i: int(x) for i, x in enumerate(input_str[::2])}
spaces = {i: int(x) for i, x in enumerate(input_str[1::2])}
space_left = {i: int(x) for i, x in enumerate(input_str[1::2])}
disk_map = {}
total = len(files)
processed = 0
for fid in reversed(files):
for i in range(fid):
if files[fid] <= space_left[i]:
pos = sum(size for f, size in files.items() if f <= i)
pos += sum(size for s, size in spaces.items() if s < i)
if space_left[i] != spaces[i]:
pos += spaces[i] - space_left[i]
space_left[i] -= files[fid]
if fid - 1 in spaces:
spaces[fid - 1] += files[fid]
space_left[fid - 1] += files[fid]
for x in range(pos, pos + files[fid]):
disk_map[x] = fid
files[fid] = 0
break
processed += 1
if processed % 10 == 0:
clear_console()
print(f"Progress: {processed}/{total} files processed ({processed/total:.1%})")
pos = 0
for fid in files:
for _ in range(files[fid]):
disk_map[pos] = fid
pos += 1
if fid in spaces:
pos += spaces[fid]
checksum = 0
for i in range(sum(int(x) for x in input_str)):
if i in disk_map:
checksum += i * disk_map[i]
return checksum
def main():
run_unoptimized = False
start_time = time.time()
input_data = parse_input('input.txt')
if run_unoptimized:
part1_start = time.time()
result1 = part_one(input_data)
part1_time = time.time() - part1_start
part2_start = time.time()
result2 = part_two(input_data)
part2_time = time.time() - part2_start
part1_opt_start = time.time()
result1_opt = part_one_optimized(input_data)
part1_opt_time = time.time() - part1_opt_start
part2_opt_start = time.time()
result2_opt = part_two_optimized(input_data)
part2_opt_time = time.time() - part2_opt_start
total_time = time.time() - start_time
clear_console()
print("=== Final Results ===")
if run_unoptimized:
print(f"Part 1: {result1} (took {part1_time:.2f}s)")
print(f"Part 2: {result2} (took {part2_time:.2f}s)")
print(f"Part 1 (Optimized): {result1_opt} (took {part1_opt_time:.2f}s)")
print(f"Part 2 (Optimized): {result2_opt} (took {part2_opt_time:.2f}s)")
print(f"\nTotal time: {total_time:.2f}s")
if __name__ == "__main__":
main()