Files
aoc/2024/day06.py
2024-12-06 18:41:36 +01:00

116 lines
3.3 KiB
Python

# AOC Day Script Day XX
# Date : 20XX.12.XX
# Python Code
# Developer : David Bandeira
import sys,time
from helpingFunctions import *
setSampleMode(False)
aocDay = identifyDay(sys.argv[0])
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:
score=0
if task == 1:
score = taskA(data)
elif task == 2:
score = taskB(data)
return score
def main():
showSampleMode()
try:
data = open(path+'/'+filename).read().strip().split('\n')
except:
print ('no inputfile found')
print ('please check file %s on path %s' % (filename, path))
quit()
st=time.time()
print ('Day '+aocDay+': Tasks 1: '+ str(task(1,data))+ ' executation time: ' + str(int((time.time()-st)*1000)) + 'ms')
st=time.time()
print ('Day '+aocDay+': Tasks 2: '+ str(task(2,data))+ ' executation time: ' + str(int((time.time()-st)*1000)) + 'ms')
if __name__ == "__main__":
main()