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')
|
||||
66
09/09-2.py
Normal file
66
09/09-2.py
Normal file
@@ -0,0 +1,66 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
#file = "./input.txt"
|
||||
file = "./ex2.txt"
|
||||
#file = "./ex1.txt"
|
||||
from time import time
|
||||
|
||||
start_time = time()
|
||||
|
||||
def input_to_list(input_str:str) -> list[list[int]]:
|
||||
out = []
|
||||
idx = 0
|
||||
for i in range(0,len(input_str),2):
|
||||
try:
|
||||
out.append([idx,int(input_str[i]),int(input_str[i+1])])
|
||||
except IndexError:
|
||||
out.append([idx,int(input_str[i]), 0])
|
||||
idx +=1
|
||||
return out
|
||||
|
||||
def de_frag(il:list[list[int]]) -> list[list[int]]:
|
||||
for i in range(len(il)-1, 0, -1):
|
||||
for j in range(len(il)):
|
||||
if j >= i:
|
||||
break
|
||||
if il[j][2] >= il[i][1]:
|
||||
|
||||
il.insert(j+1, il.pop(i))
|
||||
#print(il[j],il[j+1],il[i], end=" ----> ")
|
||||
il[i][2] += il[j+1][1]
|
||||
il[i][2] += il[j+1][2]
|
||||
il[j+1][2] = il[j][2] - il[j+1][1]
|
||||
il[j][2] = 0
|
||||
|
||||
|
||||
#print(il[j],il[j+1],il[i])
|
||||
|
||||
return il
|
||||
|
||||
def checksum(il:list[list[int]]) -> int:
|
||||
result = 0
|
||||
idx = 0
|
||||
for i in range(len(il)):
|
||||
for _ in range(il[i][1]):
|
||||
result += idx * il[i][0]
|
||||
idx +=1
|
||||
idx += il[i][2]
|
||||
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()
|
||||
|
||||
ex = input_to_list(input_string)
|
||||
#print(ex)
|
||||
de = de_frag(ex)
|
||||
print(de)
|
||||
sol = checksum(de)
|
||||
print(f'Solution: {sol}')
|
||||
print(f'Runtime: {time()-start_time:.2f} s')
|
||||
1
09/ex1.txt
Normal file
1
09/ex1.txt
Normal file
@@ -0,0 +1 @@
|
||||
12345
|
||||
1
09/ex2.txt
Normal file
1
09/ex2.txt
Normal file
@@ -0,0 +1 @@
|
||||
2333133121414131402
|
||||
1
09/input.txt
Normal file
1
09/input.txt
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user