Upd Day05 & Day06
This commit is contained in:
@@ -13,13 +13,82 @@ aocYear = identifyYear(sys.argv[0])
|
||||
|
||||
path = getPath2Data(aocDay,aocYear)
|
||||
filename = getFilename2Data(aocDay)
|
||||
|
||||
posGuard=(0,0)
|
||||
mapGrid={}
|
||||
|
||||
def createMapGrid(data):
|
||||
posY=0
|
||||
global posGuard
|
||||
global mapGrid
|
||||
for dataLine in data:
|
||||
posX = 0
|
||||
for dataPoint in dataLine:
|
||||
if dataPoint == '^':
|
||||
dataPoint = '.'
|
||||
posGuard=(posX,posY)
|
||||
mapGrid[posX,posY] = dataPoint
|
||||
posX += 1
|
||||
posY += 1
|
||||
return (posX,posY)
|
||||
|
||||
def detectNoCollition(startPoint,moveDirection):
|
||||
try:
|
||||
if moveDirection == 'N' and mapGrid[startPoint[0],startPoint[1]-1] == '#': return True
|
||||
elif moveDirection == 'S' and mapGrid[startPoint[0],startPoint[1]+1] == '#': return True
|
||||
elif moveDirection == 'W' and mapGrid[startPoint[0]-1,startPoint[1]] == '#': return True
|
||||
elif moveDirection == 'O' and mapGrid[startPoint[0]+1,startPoint[1]] == '#': return True
|
||||
else:
|
||||
return False
|
||||
except:
|
||||
pass
|
||||
|
||||
def rotateGuard(moveDirection):
|
||||
if (moveDirection == 'N'):
|
||||
return 'O'
|
||||
elif (moveDirection == 'O'):
|
||||
return 'S'
|
||||
elif (moveDirection == 'S'):
|
||||
return 'W'
|
||||
elif (moveDirection == 'W'):
|
||||
return 'N'
|
||||
|
||||
def moveGuard(startPoint,moveDirection):
|
||||
if detectNoCollition(startPoint,moveDirection):
|
||||
moveDirection = rotateGuard(moveDirection)
|
||||
global mapGrid
|
||||
mapGrid[startPoint[0],startPoint[1]] = '*'
|
||||
if moveDirection == 'N':
|
||||
newPoint=(startPoint[0],startPoint[1]-1)
|
||||
elif moveDirection == 'S':
|
||||
newPoint=(startPoint[0],startPoint[1]+1)
|
||||
elif moveDirection == 'W':
|
||||
newPoint=(startPoint[0]-1,startPoint[1])
|
||||
elif moveDirection == 'O':
|
||||
newPoint=(startPoint[0]+1,startPoint[1])
|
||||
return (newPoint,moveDirection)
|
||||
|
||||
def taskA (data) -> int:
|
||||
global posGuard
|
||||
global mapGrid
|
||||
gameScoreA = 0
|
||||
posX,posY = createMapGrid(data)
|
||||
moveDirection='N'
|
||||
|
||||
while True:
|
||||
posGuard,moveDirection = moveGuard(posGuard,moveDirection)
|
||||
if posGuard[0]>posX or posGuard[0]<0 or posGuard[1]>posY or posGuard[1]<0:
|
||||
break
|
||||
mapGridList = list(mapGrid.values())
|
||||
gameScoreA = mapGridList.count('*')-1
|
||||
return gameScoreA
|
||||
|
||||
def taskB (data) -> int:
|
||||
global posGuard
|
||||
global mapGrid
|
||||
gameScoreB = 0
|
||||
posX,posY = createMapGrid(data)
|
||||
moveDirection='N'
|
||||
return gameScoreB
|
||||
|
||||
def task(task: int,data) -> int:
|
||||
|
||||
Reference in New Issue
Block a user