Advertisement
TheAmazingDuck

AoC Day 12 Part 1

Dec 12th, 2022
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.93 KB | None | 0 0
  1. import os
  2. import string
  3. import sys
  4. from heapq import heapify, heappush, heappop
  5.  
  6. dirPath = os.path.dirname(os.path.realpath(__file__))
  7. strDataFileName = 'raw_data.txt'
  8. strTestDataFileName = 'test_data.txt'
  9. strTestData2FileName = 'test_data_2.txt'
  10.  
  11. def fnReadConfig(path, filename):
  12.     with open(os.path.join(path ,filename),"r") as raw_data_file:
  13.         data = raw_data_file.read().split('\n')
  14.     return data
  15.  
  16. raw_data = fnReadConfig(dirPath,strTestDataFileName)
  17.  
  18. aryMap = [list(x) for x in raw_data]
  19.  
  20. intColumns = len(aryMap[0])
  21.  
  22. intRows = len(aryMap)
  23.  
  24. lstNodes = []
  25.  
  26. lstAlphabet = list(string.ascii_lowercase)
  27.  
  28. dctGraph = {}
  29.  
  30. def fnGetNeighbours(row,column):
  31.     global intColumns, intRows
  32.  
  33.     #lstNeighbours [north,east,south,west]
  34.     lstNeighbours = []
  35.  
  36.     if row - 1 >= 0:
  37.         lstNorthNeighbor = [row-1,column]
  38.         lstNeighbours.append(lstNorthNeighbor)
  39.    
  40.     if column + 1 <= intColumns - 1 :
  41.         lstEastNeighbor = [row,column+1]
  42.         lstNeighbours.append(lstEastNeighbor)
  43.    
  44.     if row + 1 <= intRows - 1:
  45.         lstSouthNeighbor = [row+1,column]
  46.         lstNeighbours.append(lstSouthNeighbor)
  47.  
  48.     if column -1 >= 0:
  49.         lstWestNeighbor = [row,column-1]
  50.         lstNeighbours.append(lstWestNeighbor)
  51.  
  52.     return lstNeighbours
  53.  
  54. def fnGetNeighbourDistances(lstNode,lstNeighbours):
  55.     global aryMap, lstAlphabet, lstNodes
  56.  
  57.     dctNeighbours = {}
  58.  
  59.     strStartLetter = aryMap[lstNode[0]][lstNode[1]]
  60.  
  61.     if strStartLetter == 'S':
  62.         intStartValue = -1
  63.     elif strStartLetter == 'E':
  64.         intStartValue = 26
  65.     else:
  66.         intStartValue = lstAlphabet.index(strStartLetter)
  67.  
  68.     for neighbour in lstNeighbours:
  69.         intNeighbourNodeID = lstNodes.index(neighbour)
  70.         strNeighbourLetter = aryMap[neighbour[0]][neighbour[1]]
  71.  
  72.         if strNeighbourLetter == 'E':
  73.             intElevation = 26 - intStartValue
  74.         elif strNeighbourLetter == 'S':
  75.             intElevation = intStartValue + 1
  76.         else:
  77.             intElevation = lstAlphabet.index(strNeighbourLetter) - intStartValue
  78.  
  79.         if abs(intElevation) <= 1:
  80.             dctNeighbours[intNeighbourNodeID] = 1
  81.  
  82.     return dctNeighbours
  83.  
  84. def fnCreateNodeData():
  85.     global lstNodes
  86.  
  87.     # create infinite value
  88.     inf = sys.maxsize
  89.  
  90.     dctNodeData = {}
  91.  
  92.     for id in range(len(lstNodes)):
  93.         dctNodeData[id] = {'cost':inf,'pred':[]}
  94.  
  95.     return dctNodeData
  96.  
  97. def dijsktra(graph,src,dest):
  98.     global lstNodes
  99.  
  100.     # create dictionary of dictionary showing cost and predecessor of each node in graph
  101.     dctNodeData = fnCreateNodeData()
  102.  
  103.     # assign the cost of 0 for source node
  104.     dctNodeData[src]['cost'] = 0
  105.  
  106.     # create set of visited nodes
  107.     visited = []
  108.  
  109.     # maintain a temporary variable of the source variable which is the min of all the neighbours
  110.     temp = src
  111.  
  112.     for i in range(len(lstNodes)-1):
  113.         if temp not in visited:
  114.             # markt that this node will now have been visited
  115.             visited.append(temp)
  116.  
  117.             lstMinHeap = []
  118.  
  119.             # loop to traverse through the neighbours of the temp variable
  120.             for j in graph[temp]:
  121.                 if j not in visited:
  122.                     cost = dctNodeData[temp]['cost'] + graph[temp][j]
  123.                     if cost < dctNodeData[j]['cost']:
  124.                         dctNodeData[j]['cost'] = cost
  125.                         dctNodeData[j]['pred'] = dctNodeData[temp]['pred'] + [temp]
  126.  
  127.                     heappush(lstMinHeap,(dctNodeData[j]['cost'],j))
  128.                    
  129.  
  130.         heapify(lstMinHeap)
  131.         temp = lstMinHeap[0][1]
  132.         print(dctNodeData[1])
  133.  
  134.     print('Shortest Distance: ' + str(dctNodeData[dest]['cost']))
  135.     print('Shortest Path: ' + str(dctNodeData[dest]['pred'] + [dest]))
  136.  
  137. # Get ID and location of all nodes in map
  138. for row in range(intRows):
  139.     for column in range(intColumns):
  140.         lstNode = [row,column]
  141.         lstNodes.append(lstNode)
  142.  
  143. # Populate graph dictionary by finding neighbours of each node and the distance of these neighbours
  144. for row in range(intRows):
  145.     for column in range(intColumns):
  146.         # Empty dictionary for distances of each neighbour
  147.         dctNeighbourDistances = {}
  148.  
  149.         # Location of node to be assessed
  150.         lstNode = [row,column]
  151.  
  152.         if aryMap[row][column] == 'S':
  153.             intSourceID = lstNodes.index(lstNode)
  154.         elif aryMap[row][column] == 'E':
  155.             intDestinationID = lstNodes.index(lstNode)
  156.  
  157.         intNodeID = lstNodes.index(lstNode)
  158.  
  159.         # location of all neighbours of node being assessed
  160.         lstNeighbours = fnGetNeighbours(row,column)
  161.  
  162.         # populate distances of all neighbour nodes of node being assessed
  163.         dctNeighbourDistances = fnGetNeighbourDistances(lstNode,lstNeighbours)
  164.  
  165.         #  Append node details to graph dictionary
  166.         dctGraph[intNodeID] = dctNeighbourDistances
  167.  
  168. dijsktra(dctGraph,intSourceID,intDestinationID)
Tags: python
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement