fix: get the min value of all ranges

This commit is contained in:
starnakin 2023-12-05 10:15:20 +01:00
parent 9c79eaa6cf
commit 618324bb17

View File

@ -1,20 +1,20 @@
import threading import threading
import time import time
text: str = open("input.txt", "r").read() text: str = open("example.txt", "r").read()
_value: int = 0 _value: int = 0
_lines = text.splitlines() _lines = text.splitlines()
current_range = [] current_ranges = []
convertions = [] convertions = []
bozo = _lines[0][7:].split(" ") bozo = _lines[0][7:].split(" ")
for i, value in enumerate(bozo): for i, value in enumerate(bozo):
if (i % 2): if (i % 2):
continue continue
current_range.append((int(bozo[i]), int(bozo[i]) + int(bozo[i + 1]))) current_ranges.append((int(bozo[i]), int(bozo[i]) + int(bozo[i + 1])))
def extract_map_data(lines: [str]): def extract_map_data(lines: [str]):
lst = [] lst = []
@ -35,8 +35,6 @@ def translation(lst: list[dict], value: int):
break break
return value return value
_min = None
for i, line in enumerate(_lines): for i, line in enumerate(_lines):
if (line.endswith(" map:")): if (line.endswith(" map:")):
convertions.append(extract_map_data(_lines[i + 1:])) convertions.append(extract_map_data(_lines[i + 1:]))
@ -65,13 +63,15 @@ def get_min_in_range(lst_conversions, start, stop):
print(i - start, '/', stop - start) print(i - start, '/', stop - start)
value = do_all_convertions(lst_conversions, i) value = do_all_convertions(lst_conversions, i)
min_value = min(min_value, value) print(min_value, value, min(value, min_value))
min_value = min(value, min_value)
i += 1 i += 1
return (min_value) return (min_value)
for bozo in current_range: _min = 2000000000000000000
start, stop = bozo for current_range in current_ranges:
_min = get_min_in_range(convertions, start, stop) start, stop = current_range
_min = min(_min, get_min_in_range(convertions, start, stop))
_value = _min _value = _min