add: part2

This commit is contained in:
starnakin 2023-12-05 09:56:18 +01:00
parent 299a70970e
commit 1d1b9630c8

73
2023/day05/src/part2.py Normal file
View File

@ -0,0 +1,73 @@
text: str = open("input.txt", "r").read()
_value: int = 0
_lines = text.splitlines()
current_range = []
convertions = []
bozo = _lines[0][7:].split(" ")
for i, value in enumerate(bozo):
if (i % 2):
continue
current_range.append((int(bozo[i]), int(bozo[i]) + int(bozo[i + 1])))
def extract_map_data(lines: [str]):
lst = []
for line in lines:
if (line == ""):
break
bozo = line.split()
length = int(bozo[2])
destination = int(bozo[0]) - int(bozo[1])
source_range = range(int(bozo[1]), int(bozo[1]) + length)
lst.append({"destination": destination, "source_range": source_range})
return lst
def translation(lst: list[dict], value: int):
for map in lst:
if value in map["source_range"]:
value += map["destination"]
break
return value
_min = None
for i, line in enumerate(_lines):
if (line.endswith(" map:")):
convertions.append(extract_map_data(_lines[i + 1:]))
print(convertions)
import time
current_time = time.time()
def display_duration(interval_between_call, nb_total_values):
global current_time
old_time = current_time
current_time = time.time()
diff = int(((current_time - old_time) / interval_between_call) * nb_total_values)
print(f"{int(diff / 3600)}h {int(diff % 3600 / 60)}min {int(diff % 60)}s")
for bozo in current_range:
start, stop = bozo
while (start < stop):
if (start % 1000 == 0):
display_duration(1000, stop - start)
print(start, '/', stop)
value = start
for i, tab_convertion in enumerate(convertions):
value = translation(tab_convertion, value)
if (_min == None):
_min = value
else:
_min = min(_min, value)
start += 1
_value = _min
print(_value)