AOC/2023/day05/src/part2.py

99 lines
2.5 KiB
Python

import threading
import time
text: str = open("input.txt", "r").read()
_value: int = 0
_lines = text.splitlines()
CHUNK_SIZE = 100000000
current_ranges = []
convertions = []
bozo = _lines[0][7:].split(" ")
for i, value in enumerate(bozo):
if (i % 2):
continue
current_ranges.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()
destination = int(bozo[0])
source_start = int(bozo[1])
length = int(bozo[2])
lst.append([destination - source_start, source_start, source_start + length])
return lst
def translation(lst: list[[]], value: int):
for dst, src_start, src_stop in lst:
if src_start <= value <= src_stop:
return(value + dst)
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")
def do_all_convertions(lst_convertions: [[[]]], 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):
if ((i - start) % 1000000 == 0):
print(f"{int((i - start) * 100/(stop - start))}% ({i - start}/{stop - start})")
value = do_all_convertions(lst_conversions, i)
min_value = min(value, min_value)
i += 1
return (min_value)
values = []
def thread_process(convertions, start_value, chunk):
global values
values.append(get_min_in_range(convertions, start_value, chunk))
for current_range in current_ranges:
print("new range")
start, stop = current_range
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)):
t = threading.Thread(target=thread_process, args=(convertions, start_value, chunks[i]))
t.start()
print(f"start thread{i + 1}/{len(chunks)}")
i += 1
start_value = chunks[i - 1]
while (threading.active_count() != 1):
print(values, threading.active_count() - 1)
time.sleep(10)
print(values)
_value = min(values)
print(_value)