From 9c79eaa6cf31b9c5463d52f27f32b511844d57b9 Mon Sep 17 00:00:00 2001 From: starnakin Date: Tue, 5 Dec 2023 10:10:33 +0100 Subject: [PATCH] split code to prepare multi thread --- 2023/day05/src/part2.py | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/2023/day05/src/part2.py b/2023/day05/src/part2.py index a25153c..adc2a1b 100644 --- a/2023/day05/src/part2.py +++ b/2023/day05/src/part2.py @@ -1,3 +1,6 @@ +import threading +import time + text: str = open("input.txt", "r").read() _value: int = 0 @@ -38,9 +41,6 @@ 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): @@ -50,23 +50,28 @@ def display_duration(interval_between_call, nb_total_values): 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: [[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): + + if ((i - start) % 10000 == 0): + display_duration(10000, stop - start) + print(i - start, '/', stop - start) + + value = do_all_convertions(lst_conversions, i) + min_value = min(min_value, value) + i += 1 + return (min_value) + 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 + _min = get_min_in_range(convertions, start, stop) _value = _min