11-1 fertig

This commit is contained in:
2023-12-11 16:03:39 +01:00
parent 2f3dd824d0
commit 25175daff9
8 changed files with 350 additions and 3 deletions

2
.idea/misc.xml generated
View File

@@ -3,7 +3,7 @@
<component name="Black"> <component name="Black">
<option name="sdkName" value="Python 3.11 (AdventOfCode2023)" /> <option name="sdkName" value="Python 3.11 (AdventOfCode2023)" />
</component> </component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" project-jdk-name="Python 3.11 (AdventOfCode2023)" project-jdk-type="Python SDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_21" project-jdk-name="Python 3.10 (AdventOfCode2023)" project-jdk-type="Python SDK">
<output url="file://$PROJECT_DIR$/out" /> <output url="file://$PROJECT_DIR$/out" />
</component> </component>
</project> </project>

View File

@@ -0,0 +1,117 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from multiprocessing import Pool
from time import time
import numpy as np
from numba import njit
start = time()
seeds = np.ndarray([]) # 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
# 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()
seed_to_soil = np.asarray(seed_to_soil)
soil_to_fertilizer = np.asarray(soil_to_fertilizer)
fertilizer_to_water = np.asarray(fertilizer_to_water)
water_to_light = np.asarray(water_to_light)
light_to_temperature = np.asarray(light_to_temperature)
temperature_to_humidity = np.asarray(temperature_to_humidity)
humidity_to_location = np.asarray(humidity_to_location)
@njit
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
@njit
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}")
return min_location
if __name__ == '__main__':
num_processes = 10
input_data = []
for i in range(0, len(seeds), 2):
input_data.append((seeds[i], seeds[i+1]))
# 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)}")

View File

@@ -5,7 +5,7 @@ part_sum = 0
matrix = [] matrix = []
# read matrix like day 3 # read matrix like day 3
input_file = open("input", "r") input_file = open("input-ex41", "r")
for line in input_file: for line in input_file:
matrix.append(line.strip()) matrix.append(line.strip())
input_file.close() input_file.close()
@@ -71,12 +71,13 @@ def where_to_go(z, s, pre):
previous = (start[0], start[1]) previous = (start[0], start[1])
aktuell = [start[0]-1, start[1]] aktuell = [start[0], start[1]+1]
count = 1 count = 1
while True: while True:
new = where_to_go(aktuell[0], aktuell[1], previous) new = where_to_go(aktuell[0], aktuell[1], previous)
print(aktuell, previous, new) print(aktuell, previous, new)
matrix[aktuell[0]] = matrix[aktuell[0]][:aktuell[1]] + "X" + matrix[aktuell[0]][aktuell[1]+1:]
if new == "Ende": if new == "Ende":
break break
else: else:
@@ -87,3 +88,6 @@ while True:
print("Counter:", count) print("Counter:", count)
print("HalfCounter:", count/2) print("HalfCounter:", count/2)
for i in matrix:
print(i)

9
10/input-ex31 Normal file
View File

@@ -0,0 +1,9 @@
...........
.S-------7.
.|F-----7|.
.||.....||.
.||.....||.
.|L-7.F-J|.
.|..|.|..|.
.L--J.L--J.
...........

10
10/input-ex41 Normal file
View File

@@ -0,0 +1,10 @@
.F----7F7F7F7F-7....
.|F--7||||||||FJ....
.||.FJ||||||||L7....
FJL7L7LJLJ||LJ.L-7..
L--J.L7...LJS7F-7L7.
....F-J..F7FJ|L7L7L7
....L7.F7||L7|.L7L7|
.....|FJLJ|FJ|F7|.LJ
....FJL-7.||.||||...
....L---J.LJ.LJLJ...

57
11/11-1.py Normal file
View File

@@ -0,0 +1,57 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
data = []
# read matrix like day 3
input_file = open("input", "r")
for line in input_file:
line = line.strip()
data.append(line)
if line.count(".") == len(line): # Zeilen ohne # gleich verdoppeln
data.append(line)
input_file.close()
zeilen = len(data)
spalten = len(data[0])
# zum test mal ausgeben
for da in data:
print(da)
print(zeilen, spalten)
# leere Spalten ermitteln
freie_spalten = [1 for i in range(spalten)]
for da in data:
for i in range(spalten):
if da[i] == "#":
freie_spalten[i] = 0
print(freie_spalten)
# Spalten verbreitern
new_data = []
for da in data:
for i in range(len(freie_spalten)-1, -1, -1):
if freie_spalten[i] == 1:
da = da[:i] + ".." + da[i+1:]
new_data.append(da)
# Testausgabe neue Matrix
for da in new_data:
print(da)
zeilen = len(new_data)
spalten = len(new_data[0])
print(zeilen, spalten)
koor_list = []
for z in range(len(new_data)):
for s in range(len(new_data[0])):
if new_data[z][s] == "#":
koor_list.append([z, s])
print(koor_list)
summe = 0
for k in range(len(koor_list)):
for m in range(k+1, len(koor_list)):
summe += abs(koor_list[m][0] - koor_list[k][0])
summe += abs(koor_list[m][1] - koor_list[k][1])
print(summe)

140
11/input Normal file
View File

@@ -0,0 +1,140 @@
......................................#.....................................#......#.......#.....................#.................#........
............................................................................................................................................
.............................................#............................................................................................#.
..........................#..................................#........#............................#........................................
...#...........................#.................#............................................#...............................#.............
....................#.................................#..................................................#.........#........................
......................................#...............................................................................................#.....
...................................................................................#.............#..........................................
.......................#.........................................#......................#..................................#.....#..........
...............#................#...................#.........................#.................................#...........................
.......#..............................................................#.....................................................................
........................................#...................#.............................................#.................................
...........................#.....................#..................................................#..................#....................
.................#........................................................................#.......................#.........................
............#..................................................#...............................#........................................#...
.#................................#.........................................................................#...............................
.....................................................#...............#....................................................#.................
..........................#...................#....................................#.................#.........................#............
........#..............................#............................................................................#.......................
........................................................#................#..................................................................
...................#.............#...............................#.........................................#................................
........................#...........................#.........................#......#.....................................................#
..#........#.................#....................................................................................................#.........
.............................................#.............#.........................................#............#.........................
...............#......................................................#......................#..........................................#...
............................................................................................................................................
..................................................................................#........................................#................
..........................#........#......#..............#.....#................................................................#...........
.........................................................................................#.....#..............#.............................
....................#..........#.....................#................................................................................#.....
.#....................................................................#........#...........................................................#
..........#..............................................................................................................#..................
............................................................#......................................#........................................
...............#..........#................................................................................#................................
........................................................#..................................#.....................................#..........
......................#................#...............................................................................................#....
........#.......................................#.........................................................................#.................
.........................................................................#........#..................................#......................
#..................................#......#............................................................#...........................#........
............................................................................................................................................
......#.................................................#..............................#....................................................
.........................#....................................................#..............................................#..........#...
.................................#.............................................................................#............................
.................#..................................#................#...........................#..........................................
..............................................................#......................#......#...............................................
.............#..........................#.................................................................................................#.
...............................#........................#...................................................................................
....................................#.................................................................#..............#......................
.......#....................................................................#.................#................................#............
..........................#.................#.....#.................#....................................................#.............#....
............................................................................................................................................
............#.............................................................................................#......#................#.........
...#..........................#..........................#......................................#...........................................
............................................................................................................................................
........#.............#...................................................#.................................................................
...........................................#....................#.................#...........................#......#.........#............
............................................................................................................................................
............#....................#.............................................................#......#.....................................
.....................................................................#..................#...................................................
.....................#..............................#........#...............#........................................................#.....
....................................#...................................................................................#...................
..................................................................................................................#.........................
.....#.......#...............#..................................................................#..............................#............
.............................................#.........#....................................................................................
..................................................................#.......#................................#................................
.........................#.................................................................#.......#.................#......................
#...............................................#.........................................................................#.......#.........
...........................................#.................#................#...........................................................#.
......................#...............#............................................#........................................................
...............#..............#....................#....................................#.........................#.........................
..#..........................................................................................................#..............................
..........#...................................................................................#........#....................................
...........................#.......#.........#.........#..................#.................................................................
................................................................#......................................................#....................
............................................................................................................................................
...............................................................................................................................#............
........#...............................#............#...................................................#........#....................#....
....................#.......................................................................................................................
............#...................................#....................................#................................#.....#...............
.............................#....................................#...............................#.........................................
...#....................................................#..................#...........................#....................................
.......................#..............#..................................................#...............................#........#.........
..............#.................#............................................................................#..............................
........#..........................................#......................................................................................#.
..........................#............................................#.....#.......................................#......................
...................................#.....................#........#...............#.........#...............................................
...........................................................................................................................#................
.........................................#.............................................................#...........................#........
......#.........................#...........................#............#.....#............................................................
...............#.....#.................................#.......................................................................#............
..................................................................................................#..............#........................#.
...................................#.......#................................................................................................
..............................................................#......................#..................#.........................#.........
..............................#.................#.........................................#.................................................
..#.........................................................................................................................................
..................#..................................................#......#......................#........................#...............
...........#...........................................................................................................#..............#.....
.....................................#......................................................................................................
........................................................#.........................................................#.........................
..........................#....................................#............................................................................
............................................................................................#...............#............................#..
#..............................................#...................................#..................#.....................................
.........#.............#..................#.....................................................................#...........................
.............................................................................................................................#..............
.....................................#...........................#............#..................#.....................................#....
.................#..........#............................#..............................................................#........#..........
.........................................................................#...................#..............................................
.................................#...........................................................................#.....#........................
........................................#..........................#........................................................................
...#.....#.................................................#......................................#......#..........................#.....#.
....................................#.............................................#.........................................................
....................#.......#................#..............................#...............................................................
.....................................................................#....................#....................#............................
.................................................#................................................................................#.........
...........#...........#...................................................................................................................#
................#.......................#.........................#...................................................#.....................
...#.......................#...............................#.........................................#.........................#............
....................................................................................#.......................................................
.............................................................................................#...........................................#..
..............#...............................#................................................................#..........#.................
.......................................................#.............#.....#................................................................
............................#...................................................................#...........................................
.....#..............................#...........................................................................................#..........#
..................................................#...............#................#........................................................
..........#..........#.......................................................................................#.........#.............#......
........................................................#....................................................................#..............
.............................#...................................................................................#.......................#..
.#.........................................#...................#...........#..........................#.....................................
....................................................................#......................#................................................
...........#......................................................................#.................................#.......................
.....#.................................................................................#.......................................#............
...............................#.....#..................................................................#...................................
....................#.........................#......................................................................................#......
..................................................................................................#...........#........#....................
...............#.........................................#............#.....................................................................
..................................#......#....................................#.............................................................
...........................#........................#.....................................#..............#...............................#..
....#..............#..............................................................................................#.........................
.............#...................................................................................#..........................................
...................................................................................#..................#..................#............#.....

10
11/input-ex Normal file
View File

@@ -0,0 +1,10 @@
...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#.....