add: part 1
This commit is contained in:
42
2023/day05/src/part1.py
Normal file
42
2023/day05/src/part1.py
Normal file
@ -0,0 +1,42 @@
|
||||
text: str = open("input.txt", "r").read()
|
||||
|
||||
_value: int = 0
|
||||
|
||||
lines = text.splitlines()
|
||||
|
||||
current_values = []
|
||||
next_convertion = {}
|
||||
|
||||
for value in lines[0][7:].split(" "):
|
||||
current_values.append(int(value))
|
||||
|
||||
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
|
||||
|
||||
def translation(lst: list[dict], value: int):
|
||||
for map in lst:
|
||||
if value in map["source_range_start"]:
|
||||
value += map["destination"]
|
||||
break
|
||||
return value
|
||||
|
||||
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)
|
||||
|
||||
_value = min(current_values)
|
||||
|
||||
print(_value)
|
Reference in New Issue
Block a user