Untitled

mail@pastecode.io avatar
unknown
python
6 months ago
13 kB
3
Indexable
Never
seed_to_soil = {}
soil_to_fertilizer = {}
fertilizer_to_water = {}
water_to_light = {}
light_to_temperature = {}
temperature_to_humidity = {}
humidity_to_location = {}
file = open('advent5.csv')

first_line = True
counter = 0
for line in file:
    if first_line == True:
        seed_set = list(line[6:].split())
        print(line)
        print(seed_set)
        for i in range(int(len(seed_set)/2)):
            seed_to_soil[(int(seed_set[2*i]),int(seed_set[2*i])+int(seed_set[2*i+1]))] = (int(seed_set[2*i]),int(seed_set[2*i])+int(seed_set[2*i+1]))
        print(list(seed_to_soil.keys()))
        first_line = False
    else:
        content = line.split()
        print(content)
        if len(content)!=0:
            if content[0] == 'seed-to-soil':
                counter +=1
                print(content[0])
                key_list = list(seed_to_soil.keys())
            elif counter == 1 and content[1]!='map:':
                
                target, source, interv = int(content[0]), int(content[1]), int(content[2])
                for i,j in key_list:
                    if source <= i and j <= interv + source:
                        
                        steps = i - source
                        steps_max = j - i
                        
                        seed_to_soil[(i,j)] = (target + steps, target+ steps + steps_max)
                    elif source <= i and j > interv + source:
                        steps = i - source
                        seed_to_soil[(i, interv + source)] = (target + steps, source + interv)
                        seed_to_soil[(source + interv + 1, j)] = (source + interv + 1, j)
                        
                    elif source > i and j <= interv + source:
                        steps = i - source
                        seed_to_soil[(i,interv + source)] = (target + steps,interv + source - i + target + steps)

                    elif source > i and j <= interv + source:
                        steps = j - source
                        seed_to_soil[(source, j)] = (target,target + steps)
                        seed_to_soil[(i, source - 1)] = (i, source - 1)
                    elif source > i and j > interv + source:
                        seed_to_soil[(i, source - 1)] = (i, source - 1)
                        seed_to_soil[(source, source + interv)] = (target,target + interv)
                        seed_to_soil[(source + interv + 1, j)] = (source + interv + 1, j)

        
                        

            if content[0] == 'soil-to-fertilizer':
                counter +=1
                print(content[0])
            elif counter == 2 and content[1]!='map:':
                target, source, interv = int(content[0]), int(content[1]), int(content[2])
                for i,j in seed_to_soil.values():
                    
                    if source <= i and j <= interv + source:
                        steps = i - source
                        steps_max = j - i
                        
                        soil_to_fertilizer[(i,j)] = (target + steps, target+ steps + steps_max)
                    elif source <= i and j > interv + source:
                        steps = i - source
                        soil_to_fertilizer[(i, interv + source)] = (target + steps, source + interv)
                        soil_to_fertilizer[(source + interv + 1, j)] = (source + interv + 1, j)
                        
                    elif source > i and j <= interv + source:
                        steps = i - source
                        soil_to_fertilizer[(i,interv + source)] = (target + steps,interv + source - i + target + steps)

                    elif source > i and j <= interv + source:
                        steps = j - source
                        soil_to_fertilizer[(source, j)] = (target,target + steps)
                        soil_to_fertilizer[(i, source - 1)] = (i, source - 1)
                    elif source > i and j > interv + source:
                        soil_to_fertilizer[(i, source - 1)] = (i, source - 1)
                        soil_to_fertilizer[(source, source + interv)] = (target,target + interv)
                        soil_to_fertilizer[(source + interv + 1, j)] = (source + interv + 1, j)
                
            if content[0] == 'fertilizer-to-water':
                counter +=1
                print(content[0])
            elif counter == 3 and content[1]!='map:':
                target, source, interv = int(content[0]), int(content[1]), int(content[2])
                for i,j in soil_to_fertilizer.values():
                    if source <= i and j <= interv + source:
                        
                        steps = i - source
                        steps_max = j - i
                        
                        fertilizer_to_water[(i,j)] = (target + steps, target+ steps + steps_max)
                    elif source <= i and j > interv + source:
                        steps = i - source
                        fertilizer_to_water[(i, interv + source)] = (target + steps, source + interv)
                        fertilizer_to_water[(source + interv + 1, j)] = (source + interv + 1, j)
                        
                    elif source > i and j <= interv + source:
                        steps = i - source
                        fertilizer_to_water[(i,interv + source)] = (target + steps,interv + source - i + target + steps)

                    elif source > i and j <= interv + source:
                        steps = j - source
                        fertilizer_to_water[(source, j)] = (target,target + steps)
                        fertilizer_to_water[(i, source - 1)] = (i, source - 1)
                    elif source > i and j > interv + source:
                        fertilizer_to_water[(i, source - 1)] = (i, source - 1)
                        fertilizer_to_water[(source, source + interv)] = (target,target + interv)
                        fertilizer_to_water[(source + interv + 1, j)] = (source + interv + 1, j)
    
            if content[0] == 'water-to-light':
                counter +=1
                print(content[0])
            elif counter == 4 and content[1]!='map:':
                target, source, interv = int(content[0]), int(content[1]), int(content[2])
                for i,j in fertilizer_to_water.values():
                
                    if source <= i and j <= interv + source:
                        
                        steps = i - source
                        steps_max = j - i
                        
                        water_to_light[(i,j)] = (target + steps, target+ steps + steps_max)
                    elif source <= i and j > interv + source:
                        steps = i - source
                        water_to_light[(i, interv + source)] = (target + steps, source + interv)
                        water_to_light[(source + interv + 1, j)] = (source + interv + 1, j)
                        
                    elif source > i and j <= interv + source:
                        steps = i - source
                        water_to_light[(i,interv + source)] = (target + steps,interv + source - i + target + steps)

                    elif source > i and j <= interv + source:
                        steps = j - source
                        water_to_light[(source, j)] = (target,target + steps)
                        water_to_light[(i, source - 1)] = (i, source - 1)
                    elif source > i and j > interv + source:
                        water_to_light[(i, source - 1)] = (i, source - 1)
                        water_to_light[(source, source + interv)] = (target,target + interv)
                        water_to_light[(source + interv + 1, j)] = (source + interv + 1, j)
                    
                        
            if content[0] == 'light-to-temperature':
                counter +=1
                print(content[0])
            elif counter == 5 and content[1]!='map:':
                target, source, interv = int(content[0]), int(content[1]), int(content[2])
                for i,j in water_to_light.values():
                    if source <= i and j <= interv + source:
                        
                        steps = i - source
                        steps_max = j - i
                        
                        light_to_temperature[(i,j)] = (target + steps, target+ steps + steps_max)
                    elif source <= i and j > interv + source:
                        steps = i - source
                        light_to_temperature[(i, interv + source)] = (target + steps, source + interv)
                        light_to_temperature[(source + interv + 1, j)] = (source + interv + 1, j)
                        
                    elif source > i and j <= interv + source:
                        steps = i - source
                        light_to_temperature[(i,interv + source)] = (target + steps,interv + source - i + target + steps)

                    elif source > i and j <= interv + source:
                        steps = j - source
                        light_to_temperature[(source, j)] = (target,target + steps)
                        light_to_temperature[(i, source - 1)] = (i, source - 1)
                    elif source > i and j > interv + source:
                        light_to_temperature[(i, source - 1)] = (i, source - 1)
                        light_to_temperature[(source, source + interv)] = (target,target + interv)
                        light_to_temperature[(source + interv + 1, j)] = (source + interv + 1, j)
                        
            if content[0] == 'temperature-to-humidity':
                counter +=1
                print(content[0])
            elif counter == 6 and content[1]!='map:':
                target, source, interv = int(content[0]), int(content[1]), int(content[2])
                for i,j in light_to_temperature.values():
                    if source <= i and j <= interv + source:
                        
                        steps = i - source
                        steps_max = j - i
                        
                        temperature_to_humidity[(i,j)] = (target + steps, target+ steps + steps_max)
                    elif source <= i and j > interv + source:
                        steps = i - source
                        temperature_to_humidity[(i, interv + source)] = (target + steps, source + interv)
                        temperature_to_humidity[(source + interv + 1, j)] = (source + interv + 1, j)
                        
                    elif source > i and j <= interv + source:
                        steps = i - source
                        temperature_to_humidity[(i,interv + source)] = (target + steps,interv + source - i + target + steps)

                    elif source > i and j <= interv + source:
                        steps = j - source
                        temperature_to_humidity[(source, j)] = (target,target + steps)
                        temperature_to_humidity[(i, source - 1)] = (i, source - 1)
                    elif source > i and j > interv + source:
                        temperature_to_humidity[(i, source - 1)] = (i, source - 1)
                        temperature_to_humidity[(source, source + interv)] = (target,target + interv)
                        temperature_to_humidity[(source + interv + 1, j)] = (source + interv + 1, j)
    
            if content[0] == 'humidity-to-location':
                counter +=1
                print(content[0])
            elif counter == 7 and content[1]!='map:':
                target, source, interv = int(content[0]), int(content[1]), int(content[2])
                for i,j in temperature_to_humidity.values():
                    if source <= i and j <= interv + source:
                        
                        steps = i - source
                        steps_max = j - i
                        
                        humidity_to_location[(i,j)] = (target + steps, target+ steps + steps_max)
                    elif source <= i and j > interv + source:
                        steps = i - source
                        humidity_to_location[(i, interv + source)] = (target + steps, source + interv)
                        humidity_to_location[(source + interv + 1, j)] = (source + interv + 1, j)
                        
                    elif source > i and j <= interv + source:
                        steps = i - source
                        humidity_to_location[(i,interv + source)] = (target + steps,interv + source - i + target + steps)

                    elif source > i and j <= interv + source:
                        steps = j - source
                        humidity_to_location[(source, j)] = (target,target + steps)
                        humidity_to_location[(i, source - 1)] = (i, source - 1)
                    elif source > i and j > interv + source:
                        humidity_to_location[(i, source - 1)] = (i, source - 1)
                        humidity_to_location[(source, source + interv)] = (target,target + interv)
                        humidity_to_location[(source + interv + 1, j)] = (source + interv + 1, j)
    
Leave a Comment