Compare commits
3 Commits
299a70970e
...
9c79eaa6cf
Author | SHA1 | Date | |
---|---|---|---|
9c79eaa6cf | |||
26bf8f179a | |||
1d1b9630c8 |
78
2023/day05/src/part2.py
Normal file
78
2023/day05/src/part2.py
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
import threading
|
||||||
|
import time
|
||||||
|
|
||||||
|
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:]))
|
||||||
|
|
||||||
|
|
||||||
|
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: [[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
|
||||||
|
_min = get_min_in_range(convertions, start, stop)
|
||||||
|
|
||||||
|
_value = _min
|
||||||
|
|
||||||
|
print(_value)
|
Loading…
Reference in New Issue
Block a user