Advertisement
bob_f

AOC2021Day9.py

Sep 25th, 2023
572
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.66 KB | None | 0 0
  1. import pprint
  2.  
  3. heightMap = []
  4. lowPoints = []
  5.  
  6. def getHeight(aRow, aCol, aDirection):
  7.     if aDirection == 'N':
  8.         if aRow == 0:
  9.             return 99
  10.         else:
  11.             return heightMap[aRow - 1][aCol]
  12.     elif aDirection == 'S':
  13.         if aRow == len(heightMap) - 1:
  14.             return 99
  15.         else:
  16.             return heightMap[aRow + 1][aCol]
  17.     elif aDirection == 'E':
  18.         if aCol == len(heightMap[0]) - 1:
  19.             return 99
  20.         else:
  21.             return heightMap[aRow][aCol + 1]
  22.     elif aDirection == 'W':
  23.         if aCol == 0:
  24.             return 99
  25.         else:
  26.             return heightMap[aRow][aCol - 1]
  27.     else:
  28.         assert False, 'This is embarrassing. How did I get here?'
  29.  
  30.     assert False, 'Should not be possible to get here'
  31.  
  32. def initHeightMap(aHeightMap):
  33.     with open('scratch.txt') as INFILE:
  34.         for LINE in INFILE:
  35.             LINE = LINE.rstrip()
  36.             row = [int(x) for x in list(LINE)]    
  37.             aHeightMap.append(row)
  38.  
  39. initHeightMap(heightMap)
  40.  
  41. for row in range(len(heightMap)):
  42.     for col in range(len(heightMap[0])):
  43.         n = getHeight(row, col, 'N')
  44.         s = getHeight(row, col, 'S')
  45.         e = getHeight(row, col, 'E')
  46.         w = getHeight(row, col, 'W')
  47.  
  48.         currentHeight = heightMap[row][col]
  49.    
  50.         if currentHeight < n and currentHeight < s and currentHeight < e and currentHeight < w:
  51.             lowPoints.append((row, col))
  52.  
  53. print(lowPoints)        
  54.  
  55. sumRiskLevels = 0
  56.  
  57. for lowPoint in lowPoints:
  58.     riskLevel = heightMap[lowPoint[0]][lowPoint[1]]
  59.     riskLevel +=1
  60.     sumRiskLevels += riskLevel
  61.  
  62. print(f'{sumRiskLevels=}')    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement