Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- oceanFloor = []
- def plotLine(aCoord1, aCoord2):
- if not (aCoord1[0] == aCoord2[0] or aCoord1[1] == aCoord2[1]):
- print(f'{aCoord1} {aCoord2} is not a vertical or horizontal line')
- return
- print(f'{aCoord1} {aCoord2} is a vertical or horizontal line, OK to continue')
- xLo = min(aCoord1[0], aCoord2[0])
- xHi = max(aCoord1[0], aCoord2[0])
- yLo = min(aCoord1[1], aCoord2[1])
- yHi = max(aCoord1[1], aCoord2[1])
- if xLo < xHi:
- print('This is a horizontal line')
- for x in range(xLo, xHi + 1):
- oceanFloor[yLo][x] += 1
- else:
- print('This is a vertical line')
- for y in range(yLo, yHi + 1):
- oceanFloor[y][xLo] += 1
- def initialiseOceanFloor():
- lOceanFloor = [[0 for x in range(1000)] for y in range(1000)]
- return lOceanFloor
- def countDangerousAreas():
- countDangerousAreas = 0
- for row in range(len(oceanFloor)):
- for col in range(len(oceanFloor[0])):
- countDangerousAreas += 1 if oceanFloor[row][col] > 1 else 0
- return countDangerousAreas
- ################################################################################
- # Main program
- ################################################################################
- oceanFloor = initialiseOceanFloor()
- with open('AOC2021D5.txt') as INFILE:
- for LINE in INFILE:
- LINE = LINE.rstrip()
- if LINE.startswith('#'):
- continue
- LINE = LINE.split(' -> ')
- print(f'{LINE=}')
- coord1 = [int(x) for x in LINE[0].split(',')]
- coord2 = [int(x) for x in LINE[1].split(',')]
- plotLine(coord1, coord2)
- print(f'{countDangerousAreas()=}')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement