From 8ced78338cb27fa90b4b2bd01f854d5855930db1 Mon Sep 17 00:00:00 2001 From: starnakin Date: Tue, 5 Dec 2023 15:34:46 +0100 Subject: [PATCH] recreation of part1 --- 2023/day05/src/part1.py | 42 ++++++++++++++--------------------------- 1 file changed, 14 insertions(+), 28 deletions(-) diff --git a/2023/day05/src/part1.py b/2023/day05/src/part1.py index 4aa59a5..e47210b 100644 --- a/2023/day05/src/part1.py +++ b/2023/day05/src/part1.py @@ -4,39 +4,25 @@ _value: int = 0 lines = text.splitlines() -current_values = [] +values = [] next_convertion = {} -for value in lines[0][7:].split(" "): - current_values.append(int(value)) +values = list(map(int, lines[0][7:].split())) -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_start = range(int(bozo[1]), int(bozo[1]) + length) - lst.append({"destination": destination, "source_range_start": source_range_start}) - return lst +print(values) -def translation(lst: list[dict], value: int): - for map in lst: - if value in map["source_range_start"]: - value += map["destination"] - break - return value +for conversions in text.split("\n\n")[1:]: -lines = lines[2:] -for i, line in enumerate(lines): - lst = [] - if (line.endswith(" map:")): - lst = extract_map_data(lines[i + 1:]) - for j, value in enumerate(current_values): - current_values[j] = translation(lst, value) + _conversion = [] + for conversion in conversions.splitlines()[1:]: + _conversion.append(list(map(int, conversion.split()))) -_value = min(current_values) + for i, value in enumerate(values): + for dst, src, length in _conversion: + if (src <= value <= src + length): + values[i] = value + dst - src + break; + +_value = min(values) print(_value) \ No newline at end of file