05-2 fertig
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
part_sum = 0
|
||||
matrix = []
|
||||
|
||||
input_file = open("input.txt", "r")
|
||||
input_file = open("input-ex.txt", "r")
|
||||
for line in input_file:
|
||||
matrix.append(line.strip())
|
||||
input_file.close()
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
matrix = []
|
||||
input_file = open("input.txt", "r")
|
||||
input_file = open("input-ex.txt", "r")
|
||||
for line in input_file:
|
||||
matrix.append(line.strip())
|
||||
input_file.close()
|
||||
|
||||
123
05/05-2-multiprocessig-pool.py
Normal file
123
05/05-2-multiprocessig-pool.py
Normal file
@@ -0,0 +1,123 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
from multiprocessing import Process
|
||||
from time import time
|
||||
|
||||
start = time()
|
||||
|
||||
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()
|
||||
|
||||
|
||||
def find_min(seed, ra):
|
||||
min_location = - 1
|
||||
print(f"gestartet.. Seed: {seed} Range: {ra}")
|
||||
for s in range(seed, seed+ra):
|
||||
soil = find_dest(s, 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
|
||||
print(f"beendet.. Seed: {seed} Range: {ra} MinLocation: {min_location}")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
p1 = Process(target=find_min, args=(seeds[0], seeds[1]))
|
||||
p2 = Process(target=find_min, args=(seeds[2], seeds[3]))
|
||||
p3 = Process(target=find_min, args=(seeds[4], seeds[5]))
|
||||
p4 = Process(target=find_min, args=(seeds[6], seeds[7]))
|
||||
p5 = Process(target=find_min, args=(seeds[8], seeds[9]))
|
||||
p6 = Process(target=find_min, args=(seeds[10], seeds[11]))
|
||||
p7 = Process(target=find_min, args=(seeds[12], seeds[13]))
|
||||
p8 = Process(target=find_min, args=(seeds[14], seeds[15]))
|
||||
p9 = Process(target=find_min, args=(seeds[16], seeds[17]))
|
||||
p10 = Process(target=find_min, args=(seeds[18], seeds[19]))
|
||||
|
||||
p1.start()
|
||||
p2.start()
|
||||
p3.start()
|
||||
p4.start()
|
||||
p5.start()
|
||||
p6.start()
|
||||
p7.start()
|
||||
p8.start()
|
||||
p9.start()
|
||||
p10.start()
|
||||
|
||||
p1.join()
|
||||
p2.join()
|
||||
p3.join()
|
||||
p4.join()
|
||||
p5.join()
|
||||
p6.join()
|
||||
p7.join()
|
||||
p8.join()
|
||||
p9.join()
|
||||
p10.join()
|
||||
@@ -1,6 +1,6 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
from multiprocessing import Process
|
||||
from multiprocessing import Pool
|
||||
from time import time
|
||||
|
||||
start = time()
|
||||
@@ -72,8 +72,8 @@ input_file.close()
|
||||
|
||||
def find_min(seed, ra):
|
||||
min_location = - 1
|
||||
print(f"gestartet.. Seed: {seed} Range: {ra}")
|
||||
for s in range(seed, ra):
|
||||
print(f"gestartet: Seed: {seed} Range: {ra}")
|
||||
for s in range(seed, seed + ra):
|
||||
soil = find_dest(s, seed_to_soil)
|
||||
fertilizer = find_dest(soil, soil_to_fertilizer)
|
||||
water = find_dest(fertilizer, fertilizer_to_water)
|
||||
@@ -86,38 +86,23 @@ def find_min(seed, ra):
|
||||
elif location < min_location:
|
||||
min_location = location
|
||||
print(f"beendet.. Seed: {seed} Range: {ra} MinLocation: {min_location}")
|
||||
return min_location
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
p1 = Process(target=find_min, args=(seeds[0], seeds[1]))
|
||||
p2 = Process(target=find_min, args=(seeds[2], seeds[3]))
|
||||
p3 = Process(target=find_min, args=(seeds[4], seeds[5]))
|
||||
p4 = Process(target=find_min, args=(seeds[6], seeds[7]))
|
||||
p5 = Process(target=find_min, args=(seeds[8], seeds[9]))
|
||||
p6 = Process(target=find_min, args=(seeds[10], seeds[11]))
|
||||
p7 = Process(target=find_min, args=(seeds[12], seeds[13]))
|
||||
p8 = Process(target=find_min, args=(seeds[14], seeds[15]))
|
||||
p9 = Process(target=find_min, args=(seeds[16], seeds[17]))
|
||||
p10 = Process(target=find_min, args=(seeds[18], seeds[19]))
|
||||
num_processes = 10
|
||||
|
||||
p1.start()
|
||||
p2.start()
|
||||
p3.start()
|
||||
p4.start()
|
||||
p5.start()
|
||||
p6.start()
|
||||
p7.start()
|
||||
p8.start()
|
||||
p9.start()
|
||||
p10.start()
|
||||
input_data = []
|
||||
for i in range(0, len(seeds), 2):
|
||||
input_data.append((seeds[i], seeds[i+1]))
|
||||
|
||||
p1.join()
|
||||
p2.join()
|
||||
p3.join()
|
||||
p4.join()
|
||||
p5.join()
|
||||
p6.join()
|
||||
p7.join()
|
||||
p8.join()
|
||||
p9.join()
|
||||
p10.join()
|
||||
# input_data manuell erstellt:
|
||||
# input_data = [(seeds[0], seeds[1]), (seeds[2], seeds[3]), (seeds[4], seeds[5]), (seeds[6], seeds[7]),
|
||||
# (seeds[8], seeds[9]), (seeds[10], seeds[11]), (seeds[12], seeds[13]), (seeds[14], seeds[15]),
|
||||
# (seeds[16], seeds[17]), (seeds[18], seeds[19])]
|
||||
|
||||
with Pool(processes=num_processes) as pool:
|
||||
results = pool.starmap(find_min, input_data)
|
||||
print(results)
|
||||
print(f"kleinste Location: {min(results)}")
|
||||
print(f"Script beendet! Benötigte Zeit in Sekunden: {int(time()-start)}")
|
||||
|
||||
BIN
05/Solution.PNG
Normal file
BIN
05/Solution.PNG
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 86 KiB |
@@ -5,7 +5,7 @@
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<excludeFolder url="file://$MODULE_DIR$/venv" />
|
||||
</content>
|
||||
<orderEntry type="jdk" jdkName="Python 3.12" jdkType="Python SDK" />
|
||||
<orderEntry type="jdk" jdkName="Python 3.11 (AdventOfCode2023)" jdkType="Python SDK" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
Reference in New Issue
Block a user