Umstellen der Auswertungslogik
This commit is contained in:
@@ -1,45 +0,0 @@
|
||||
from vorlesung.L08_graphen.graph import Graph, AdjacencyMatrixGraph
|
||||
from utils.project_dir import get_path
|
||||
|
||||
graph = AdjacencyMatrixGraph()
|
||||
start = ""
|
||||
end = ""
|
||||
|
||||
def read_file(filename: str = "data/aoc2212.txt"):
|
||||
"""Read a file and return the content as a string."""
|
||||
|
||||
def adjust_char(char):
|
||||
"""Adjust character for comparison."""
|
||||
if char == 'S':
|
||||
return 'a'
|
||||
elif char == 'E':
|
||||
return 'z'
|
||||
return char
|
||||
|
||||
global start, end
|
||||
with open(get_path(filename), "r") as file:
|
||||
quest = file.read().strip().splitlines()
|
||||
for row, line in enumerate(quest):
|
||||
for col, char in enumerate(line):
|
||||
label = f"{row},{col}"
|
||||
graph.insert_vertex(label)
|
||||
if char == "S":
|
||||
start = label
|
||||
if char == "E":
|
||||
end = label
|
||||
for row, line in enumerate(quest):
|
||||
for col, char in enumerate(line):
|
||||
for neighbor in [(row - 1, col), (row, col - 1), (row + 1, col), (row, col + 1)]:
|
||||
if 0 <= neighbor[0] < len(quest) and 0 <= neighbor[1] < len(line):
|
||||
if ord(adjust_char(quest[neighbor[0]][neighbor[1]])) <= ord(adjust_char(char)) + 1:
|
||||
label1 = f"{row},{col}"
|
||||
label2 = f"{neighbor[0]},{neighbor[1]}"
|
||||
graph.connect(label1, label2)
|
||||
|
||||
|
||||
# Lösung des Adventskalenders 2022, Tag 12
|
||||
read_file("data/aoc2212test.txt")
|
||||
graph.graph()
|
||||
distance_map, predecessor_map = graph.bfs(start)
|
||||
print(distance_map[graph.get_vertex(end)])
|
||||
print(graph.path(end, predecessor_map))
|
||||
@@ -5,8 +5,8 @@ import graphviz
|
||||
import math
|
||||
import heapq
|
||||
from datetime import datetime
|
||||
from utils.project_dir import get_path
|
||||
from utils.priority_queue import PriorityQueue
|
||||
from utils.algo_path import path
|
||||
from utils.algo_priority_queue import PriorityQueue
|
||||
from vorlesung.L09_mst.disjoint import DisjointValue
|
||||
|
||||
|
||||
@@ -155,7 +155,7 @@ class Graph:
|
||||
dot.edge(str(id(self.get_vertex(edge[0]))), str(id(self.get_vertex(edge[1]))), label=str(edge[2]))
|
||||
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
||||
filename = f"{filename}_{timestamp}.gv"
|
||||
filename = get_path(filename)
|
||||
filename = path(filename)
|
||||
dot.render(filename)
|
||||
|
||||
def dijkstra(self, start_name: str) -> tuple[dict[Vertex, float], dict[Vertex, Vertex | None]]:
|
||||
|
||||
Reference in New Issue
Block a user