142 lines
4.6 KiB
Python
142 lines
4.6 KiB
Python
from utils import load_data
|
|
import math
|
|
|
|
class Map:
|
|
def __init__(self, target: int, source: int, length: int):
|
|
self.target: int = target
|
|
self.source: int = source
|
|
self.length: int = length
|
|
|
|
def __str__(self) -> str:
|
|
return str(self.source) + "|" + str(self.target) + "|" + str(self.length)
|
|
|
|
def __repr__(self) -> str:
|
|
return str(self)
|
|
|
|
def get_map(input: list[str]) -> list[Map]:
|
|
map_list = []
|
|
for line in input:
|
|
split = line.split(" ")
|
|
map = Map(int(split[0]), int(split[1]), int(split[2]))
|
|
map_list.append(map)
|
|
|
|
return map_list
|
|
|
|
|
|
def question_1(data):
|
|
|
|
seed_to_soil_map = get_map(data[3:9])
|
|
soil_to_fertilizer_map = get_map(data[11:54])
|
|
fertilizer_to_water_map = get_map(data[56:95])
|
|
water_to_light_map = get_map(data[97:144])
|
|
light_to_temperature_map = get_map(data[146:173])
|
|
temperature_to_humidity_map = get_map(data[175:183])
|
|
humidity_to_location_map = get_map(data[185:201])
|
|
|
|
# seed_to_soil_map = get_map(data[3:5])
|
|
# soil_to_fertilizer_map = get_map(data[7:10])
|
|
# fertilizer_to_water_map = get_map(data[12:16])
|
|
# water_to_light_map = get_map(data[18:20])
|
|
# light_to_temperature_map = get_map(data[22:25])
|
|
# temperature_to_humidity_map = get_map(data[27:29])
|
|
# humidity_to_location_map = get_map(data[31:33])
|
|
|
|
maps = [seed_to_soil_map, soil_to_fertilizer_map, fertilizer_to_water_map, water_to_light_map, light_to_temperature_map, temperature_to_humidity_map, humidity_to_location_map]
|
|
|
|
seeds = data[0].split(":")[1].split(" ")[1:]
|
|
seed_locations = {}
|
|
|
|
# Check for all seeds what the location is
|
|
for seed in seeds:
|
|
# cast to int
|
|
seed = int(seed)
|
|
source = seed
|
|
|
|
# Move the seed through each maplist
|
|
for map_list in maps:
|
|
|
|
# check if the source is mapped to some destination
|
|
in_map = False
|
|
for map in map_list:
|
|
if source >= map.source and source <= map.source + map.length - 1:
|
|
in_map = True
|
|
found_map = map
|
|
|
|
# If the source is mapped explicitly, find the target
|
|
if in_map:
|
|
difference = source - found_map.source
|
|
source = found_map.target + difference
|
|
|
|
|
|
# After moving through all map_lists, append the final location
|
|
seed_locations[seed] = source
|
|
|
|
min = math.inf
|
|
for key in seed_locations:
|
|
if seed_locations[key] < min:
|
|
min = seed_locations[key]
|
|
|
|
print(f"Answer to question 1 is: {min}")
|
|
|
|
def question_2(data: list[str]):
|
|
|
|
seed_to_soil_map = get_map(data[3:9])
|
|
soil_to_fertilizer_map = get_map(data[11:54])
|
|
fertilizer_to_water_map = get_map(data[56:95])
|
|
water_to_light_map = get_map(data[97:144])
|
|
light_to_temperature_map = get_map(data[146:173])
|
|
temperature_to_humidity_map = get_map(data[175:183])
|
|
humidity_to_location_map = get_map(data[185:201])
|
|
|
|
maps = [seed_to_soil_map, soil_to_fertilizer_map, fertilizer_to_water_map, water_to_light_map, light_to_temperature_map, temperature_to_humidity_map, humidity_to_location_map]
|
|
|
|
seeds = []
|
|
seed_ranges = data[0].split(":")[1].split(" ")[1:]
|
|
for index, value in enumerate(seed_ranges):
|
|
if index % 2 == 0:
|
|
start = int(value)
|
|
end = start + int(seed_ranges[index + 1])
|
|
seeds += range(start, end)
|
|
print(seeds)
|
|
seed_locations = {}
|
|
return
|
|
# Check for all seeds what the location is
|
|
for seed in seeds:
|
|
# cast to int
|
|
seed = int(seed)
|
|
source = seed
|
|
|
|
# Move the seed through each maplist
|
|
for map_list in maps:
|
|
|
|
# check if the source is mapped to some destination
|
|
in_map = False
|
|
for map in map_list:
|
|
if source >= map.source and source <= map.source + map.length - 1:
|
|
in_map = True
|
|
found_map = map
|
|
|
|
# If the source is mapped explicitly, find the target
|
|
if in_map:
|
|
difference = source - found_map.source
|
|
source = found_map.target + difference
|
|
|
|
|
|
# After moving through all map_lists, append the final location
|
|
seed_locations[seed] = source
|
|
|
|
min = math.inf
|
|
for key in seed_locations:
|
|
if seed_locations[key] < min:
|
|
min = seed_locations[key]
|
|
|
|
print(f"Answer to question 1 is: {min}")
|
|
|
|
|
|
print(f"Answer to question 2 is: {sum}")
|
|
|
|
if __name__ == "__main__":
|
|
filepath = "Data/day5.txt"
|
|
data = load_data(filepath)
|
|
# question_1(data)
|
|
question_2(data) |