From 23a9d663e428db5c67b881eeea12011da6c68132 Mon Sep 17 00:00:00 2001 From: starnakin Date: Tue, 5 Dec 2023 14:34:15 +0100 Subject: [PATCH] opti use simple operation --- 2023/day05/src/part2.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/2023/day05/src/part2.py b/2023/day05/src/part2.py index ec15d1e..0420b70 100644 --- a/2023/day05/src/part2.py +++ b/2023/day05/src/part2.py @@ -23,17 +23,16 @@ def extract_map_data(lines: [str]): if (line == ""): break bozo = line.split() + destination = int(bozo[0]) + source_start = int(bozo[1]) 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}) + lst.append([destination - source_start, source_start, source_start + length]) return lst -def translation(lst: list[dict], value: int): - for map in lst: - if value in map["source_range"]: - value += map["destination"] - break +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): @@ -48,7 +47,7 @@ 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): +def do_all_convertions(lst_convertions: [[[]]], value): for i, tab_convertion in enumerate(convertions): value = translation(tab_convertion, value) return value @@ -58,9 +57,8 @@ def get_min_in_range(lst_conversions, start, stop): i = start + 1; while (i != stop): if ((i - start) % 1000000 == 0): - print(f"{int((i - start)/(stop - start))}% ({i - start}/{stop - start})") + print(f"{int((i - start) * 100/(stop - start))}% ({i - start}/{stop - start})") value = do_all_convertions(lst_conversions, i) - #print(min_value, value, min(value, min_value)) min_value = min(value, min_value) i += 1 return (min_value) @@ -95,6 +93,7 @@ while (threading.active_count() != 1): print(values, threading.active_count() - 1) time.sleep(10) +print(values) _value = min(values) print(_value) \ No newline at end of file