-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
66 lines (56 loc) · 1.54 KB
/
main.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
with open('input.txt') as file:
data = list(file.read().strip())
arr = []
numbers_arr = []
for i in range(len(data)):
if i % 2 == 0:
num = int(data[i])
numbers_arr.append((len(arr), len(arr) + num))
for _ in range(num):
arr.append(i // 2)
else:
num = int(data[i])
for _ in range(num):
arr.append('.')
stripped_arr = [x for x in arr if x != '.'][::-1]
filled = 0
arr_1 = arr.copy()
for i in range(len(arr)):
if arr_1[i] == '.':
arr_1[i] = stripped_arr[filled]
filled += 1
arr_1 = arr_1[:len(stripped_arr)]
total = 0
for i in range(len(arr_1)):
total += i * arr_1[i]
print(total)
closest_free_space = 0
def find_closest_space(length):
global closest_free_space
closest_free_space = 0
found_free = False
i = closest_free_space
curr_length = 0
while i < len(arr):
if arr[i] == '.':
if not found_free:
found_free = True
closest_free_space = i
curr_length += 1
else:
curr_length = 0
if curr_length >= length:
return i - length + 1
i += 1
return None
for start, end in numbers_arr[::-1]:
length = end - start
leftmost_pos = find_closest_space(length)
if leftmost_pos is not None and leftmost_pos < start:
for i in range(length):
arr[leftmost_pos + i], arr[start + i] = arr[start + i], arr[leftmost_pos + i]
total = 0
for i in range(len(arr)):
if arr[i] != '.':
total += i * arr[i]
print(total)