2023-12-05 04:10:33 -05:00
|
|
|
import threading
|
|
|
|
import time
|
|
|
|
|
2023-12-05 04:49:36 -05:00
|
|
|
text: str = open("input.txt", "r").read()
|
2023-12-05 03:56:18 -05:00
|
|
|
|
|
|
|
_value: int = 0
|
|
|
|
|
|
|
|
_lines = text.splitlines()
|
|
|
|
|
2023-12-05 04:15:20 -05:00
|
|
|
current_ranges = []
|
2023-12-05 03:56:18 -05:00
|
|
|
convertions = []
|
|
|
|
|
|
|
|
bozo = _lines[0][7:].split(" ")
|
|
|
|
for i, value in enumerate(bozo):
|
|
|
|
if (i % 2):
|
|
|
|
continue
|
2023-12-05 04:15:20 -05:00
|
|
|
current_ranges.append((int(bozo[i]), int(bozo[i]) + int(bozo[i + 1])))
|
2023-12-05 03:56:18 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
for i, line in enumerate(_lines):
|
|
|
|
if (line.endswith(" map:")):
|
|
|
|
convertions.append(extract_map_data(_lines[i + 1:]))
|
|
|
|
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
2023-12-05 04:10:33 -05:00
|
|
|
def do_all_convertions(lst_convertions: [[dict]], value):
|
|
|
|
for i, tab_convertion in enumerate(convertions):
|
|
|
|
value = translation(tab_convertion, value)
|
|
|
|
return value
|
|
|
|
|
|
|
|
def get_min_in_range(lst_conversions, start, stop):
|
|
|
|
min_value = do_all_convertions(lst_conversions, start)
|
|
|
|
i = start + 1;
|
|
|
|
while (i != stop):
|
|
|
|
value = do_all_convertions(lst_conversions, i)
|
2023-12-05 04:37:17 -05:00
|
|
|
#print(min_value, value, min(value, min_value))
|
2023-12-05 04:15:20 -05:00
|
|
|
min_value = min(value, min_value)
|
2023-12-05 04:10:33 -05:00
|
|
|
i += 1
|
|
|
|
return (min_value)
|
2023-12-05 03:56:18 -05:00
|
|
|
|
2023-12-05 04:37:17 -05:00
|
|
|
values = []
|
2023-12-05 04:49:36 -05:00
|
|
|
|
|
|
|
def thread_process(convertions, start_value, chunk):
|
|
|
|
global values
|
|
|
|
values.append(get_min_in_range(convertions, start_value, chunk))
|
|
|
|
|
|
|
|
CHUNK_SIZE = 10
|
2023-12-05 04:15:20 -05:00
|
|
|
for current_range in current_ranges:
|
|
|
|
start, stop = current_range
|
2023-12-05 04:37:17 -05:00
|
|
|
i = start
|
|
|
|
chunks = []
|
|
|
|
while(i < stop):
|
|
|
|
i += CHUNK_SIZE
|
|
|
|
if (i >= stop):
|
|
|
|
chunks.append(stop)
|
|
|
|
else:
|
|
|
|
chunks.append(i)
|
|
|
|
start_value = start
|
|
|
|
i = 0
|
|
|
|
while (i < len(chunks)):
|
2023-12-05 04:49:36 -05:00
|
|
|
t = threading.Thread(target=thread_process, args=(convertions, start_value, chunks[i]))
|
|
|
|
t.start()
|
|
|
|
display_duration(1, len(chunks))
|
|
|
|
print(i, '/', len(chunks))
|
2023-12-05 04:37:17 -05:00
|
|
|
i += 1
|
|
|
|
start_value = chunks[i - 1]
|
2023-12-05 03:56:18 -05:00
|
|
|
|
2023-12-05 04:37:17 -05:00
|
|
|
_value = min(values)
|
2023-12-05 03:56:18 -05:00
|
|
|
|
|
|
|
print(_value)
|