05-2 fertig
This commit is contained in:
85
05/05-2.py
Normal file
85
05/05-2.py
Normal file
@@ -0,0 +1,85 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
seeds = [] # 0
|
||||
seed_to_soil = [] # 1
|
||||
soil_to_fertilizer = [] # 2
|
||||
fertilizer_to_water = [] # 3
|
||||
water_to_light = [] # 4
|
||||
light_to_temperature = [] # 5
|
||||
temperature_to_humidity = [] # 6
|
||||
humidity_to_location = [] # 7
|
||||
|
||||
list_number = 0
|
||||
|
||||
|
||||
def find_dest(num, map_list):
|
||||
for m in map_list:
|
||||
if m[1] <= num < m[1] + m[2]:
|
||||
return num-m[1]+m[0]
|
||||
return num
|
||||
|
||||
|
||||
# parse input
|
||||
input_file = open("input", "r")
|
||||
for line in input_file:
|
||||
line = line.strip()
|
||||
if line == "":
|
||||
continue
|
||||
if line.startswith('seeds: '):
|
||||
seeds = [int(x) for x in line.replace('seeds: ', '').split()]
|
||||
if line == "seed-to-soil map:":
|
||||
list_number = 1
|
||||
continue
|
||||
if line == "soil-to-fertilizer map:":
|
||||
list_number = 2
|
||||
continue
|
||||
if line == "fertilizer-to-water map:":
|
||||
list_number = 3
|
||||
continue
|
||||
if line == "water-to-light map:":
|
||||
list_number = 4
|
||||
continue
|
||||
if line == "light-to-temperature map:":
|
||||
list_number = 5
|
||||
continue
|
||||
if line == "temperature-to-humidity map:":
|
||||
list_number = 6
|
||||
continue
|
||||
if line == "humidity-to-location map:":
|
||||
list_number = 7
|
||||
continue
|
||||
if list_number == 1:
|
||||
seed_to_soil.append([int(x) for x in line.split()])
|
||||
elif list_number == 2:
|
||||
soil_to_fertilizer.append([int(x) for x in line.split()])
|
||||
elif list_number == 3:
|
||||
fertilizer_to_water.append([int(x) for x in line.split()])
|
||||
elif list_number == 4:
|
||||
water_to_light.append([int(x) for x in line.split()])
|
||||
elif list_number == 5:
|
||||
light_to_temperature.append([int(x) for x in line.split()])
|
||||
elif list_number == 6:
|
||||
temperature_to_humidity.append([int(x) for x in line.split()])
|
||||
elif list_number == 7:
|
||||
humidity_to_location.append([int(x) for x in line.split()])
|
||||
input_file.close()
|
||||
|
||||
# Ausgaben
|
||||
min_location = -1
|
||||
for i in range(0, len(seeds), 2):
|
||||
print(f"Seed: {seeds[i]} Range: {seeds[i+1]}")
|
||||
for seed in range(seeds[i], seeds[i] + seeds[i+1]):
|
||||
soil = find_dest(seed, seed_to_soil)
|
||||
fertilizer = find_dest(soil, soil_to_fertilizer)
|
||||
water = find_dest(fertilizer, fertilizer_to_water)
|
||||
light = find_dest(water, water_to_light)
|
||||
temperature = find_dest(light, light_to_temperature)
|
||||
humidity = find_dest(temperature, temperature_to_humidity)
|
||||
location = find_dest(humidity, humidity_to_location)
|
||||
if min_location == -1:
|
||||
min_location = location
|
||||
elif location < min_location:
|
||||
min_location = location
|
||||
|
||||
# #### Final
|
||||
print(f"kleinste Location: {min_location}")
|
||||
Reference in New Issue
Block a user