Advertisement
bob_f

AOC2021D5P1.py

Sep 22nd, 2023
817
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.69 KB | None | 0 0
  1. oceanFloor = []
  2.  
  3. def plotLine(aCoord1, aCoord2):
  4.     if not (aCoord1[0] == aCoord2[0] or aCoord1[1] == aCoord2[1]):
  5.         print(f'{aCoord1} {aCoord2} is not a vertical or horizontal line')
  6.         return
  7.    
  8.     print(f'{aCoord1} {aCoord2} is a vertical or horizontal line, OK to continue')
  9.  
  10.     xLo = min(aCoord1[0], aCoord2[0])
  11.     xHi = max(aCoord1[0], aCoord2[0])
  12.     yLo = min(aCoord1[1], aCoord2[1])
  13.     yHi = max(aCoord1[1], aCoord2[1])
  14.    
  15.     if xLo < xHi:
  16.         print('This is a horizontal line')    
  17.  
  18.         for x in range(xLo, xHi + 1):
  19.             oceanFloor[yLo][x] += 1
  20.     else:
  21.         print('This is a vertical line')    
  22.  
  23.         for y in range(yLo, yHi + 1):
  24.             oceanFloor[y][xLo] += 1
  25.  
  26. def initialiseOceanFloor():
  27.     lOceanFloor = [[0 for x in range(1000)] for y in range(1000)]
  28.     return lOceanFloor
  29.  
  30. def countDangerousAreas():
  31.     countDangerousAreas = 0
  32.  
  33.     for row in range(len(oceanFloor)):
  34.         for col in range(len(oceanFloor[0])):
  35.             countDangerousAreas += 1 if oceanFloor[row][col] > 1 else 0
  36.  
  37.     return countDangerousAreas
  38.  
  39. ################################################################################
  40. # Main program
  41. ################################################################################
  42.  
  43. oceanFloor = initialiseOceanFloor()      
  44.  
  45. with open('AOC2021D5.txt') as INFILE:
  46.    for LINE in INFILE:
  47.       LINE = LINE.rstrip()
  48.  
  49.       if LINE.startswith('#'):
  50.           continue
  51.  
  52.       LINE = LINE.split(' -> ')
  53.       print(f'{LINE=}')
  54.       coord1 = [int(x) for x in LINE[0].split(',')]
  55.       coord2 = [int(x) for x in LINE[1].split(',')]
  56.       plotLine(coord1, coord2)
  57.  
  58. print(f'{countDangerousAreas()=}')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement