9-1
This commit is contained in:
68
09/09-1.py
Normal file
68
09/09-1.py
Normal file
@@ -0,0 +1,68 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
file = "./input.txt"
|
||||
#file = "./ex2.txt"
|
||||
#file = "./ex1.txt"
|
||||
from time import time
|
||||
|
||||
start_time = time()
|
||||
|
||||
def extend_input_to_list(input_str:str) -> list[int]:
|
||||
out = []
|
||||
free = False
|
||||
idn = 0
|
||||
for element in input_str:
|
||||
for e in range(int(element)):
|
||||
if free:
|
||||
out.append(-1)
|
||||
else:
|
||||
out.append(idn)
|
||||
if not free:
|
||||
idn += 1
|
||||
free = not free
|
||||
return out
|
||||
|
||||
def de_frag(input_list:list[int]) -> list[int]:
|
||||
out = []
|
||||
i_less_than_j = True
|
||||
for i in range(len(input_list)):
|
||||
if input_list[i] != -1:
|
||||
out.append(input_list[i])
|
||||
elif not i_less_than_j:
|
||||
break
|
||||
else:
|
||||
for j in range(len(input_list)-1, 0, -1):
|
||||
if j <= i:
|
||||
i_less_than_j = False
|
||||
break
|
||||
elif input_list[j] == -1:
|
||||
continue
|
||||
else:
|
||||
out.append(input_list[j])
|
||||
input_list[j] = -1
|
||||
break
|
||||
return out
|
||||
|
||||
def checksum(input_list:list[int]) -> int:
|
||||
result = 0
|
||||
for i in range(len(input_list)):
|
||||
result += i * input_list[i]
|
||||
return result
|
||||
|
||||
if __name__ == "__main__":
|
||||
input_string = ""
|
||||
input_file = open(file, "r")
|
||||
for line in input_file:
|
||||
line = line.strip()
|
||||
if line == "":
|
||||
continue
|
||||
input_string += line
|
||||
input_file.close()
|
||||
|
||||
#print(extend_input_to_list(input_string))
|
||||
ex = extend_input_to_list(input_string)
|
||||
#print(de_frag(extend_input_to_list(input_string)))
|
||||
de = de_frag(ex)
|
||||
sol = checksum(de)
|
||||
print(f'Solution: {sol}')
|
||||
print(f'Runtime: {time()-start_time:.2f} s')
|
||||
Reference in New Issue
Block a user