81 lines
2.3 KiB
Python
81 lines
2.3 KiB
Python
# AOC Day Script Day 03
|
|
# Date : 2024.11.09
|
|
# 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)
|
|
|
|
|
|
def movePoint(actualPoint,movement):
|
|
x0,y0 = actualPoint
|
|
x1,y1 = movement
|
|
return ((x0+x1,y0+y1))
|
|
|
|
def visitHouses(actualPoint,movements):
|
|
visitedHouses=[]
|
|
visitedHouses.append(actualPoint)
|
|
for move in movements:
|
|
if move == '^': actualPoint = movePoint(actualPoint,(0,1))
|
|
elif move == 'v': actualPoint = movePoint(actualPoint,(0,-1))
|
|
elif move == '>': actualPoint = movePoint(actualPoint,(1,0))
|
|
elif move == '<': actualPoint = movePoint(actualPoint,(-1,0))
|
|
visitedHouses.append(actualPoint)
|
|
return visitedHouses
|
|
|
|
def taskA (data) -> int:
|
|
gameScoreA = 0
|
|
for dataRow in data:
|
|
visitedHouses=visitHouses((0,0),dataRow)
|
|
gameScoreA=len(list(set(visitedHouses)))
|
|
return gameScoreA
|
|
|
|
def taskB (data) -> int:
|
|
gameScoreB = 0
|
|
for dataRow in data:
|
|
rowNr=0
|
|
santaMoves=[]
|
|
roboMoves=[]
|
|
for dataElem in dataRow:
|
|
rowNr += 1
|
|
if ( rowNr % 2 == 0 ):
|
|
roboMoves.append(dataElem)
|
|
else:
|
|
santaMoves.append(dataElem)
|
|
santaVisited=visitHouses((0,0),santaMoves)
|
|
roboVisited=visitHouses((0,0),roboMoves)
|
|
visitedHouses = santaVisited+roboVisited
|
|
gameScoreB=len(list(set(visitedHouses)))
|
|
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() |