Files
aoc/2024/day11.py
2024-12-14 21:41:56 +01:00

85 lines
2.2 KiB
Python

# AOC Day Script Day XX
# Date : 20XX.12.XX
# Python Code
# Developer : David Bandeira
import sys,time
from helpingFunctions import *
from functools import lru_cache
setSampleMode(False)
aocDay = identifyDay(sys.argv[0])
aocYear = identifyYear(sys.argv[0])
path = getPath2Data(aocDay,aocYear)
filename = getFilename2Data(aocDay)
lookup={}
@lru_cache(maxsize=None)
def laserStone(stone):
if stone == 0:
return [1]
elif len(str(stone))%2 == 0:
l = len(str(stone))
stone=str(stone)
j = int(l/2)
return [int(stone[:j]),int(stone[j:])]
else:
return [int(stone)*2024]
def taskA (data) -> int:
gameScoreA = 0
stoneRow = [int(stone) for stone in data[0].split(' ')]
for i in range(25):
print ("Loop:" + str(i) + " = " + str(len(stoneRow)))
listStone=[]
for stone in stoneRow:
newStone= laserStone(stone)
listStone = listStone + newStone
stoneRow = listStone
gameScoreA = len(stoneRow)
return gameScoreA
def taskB (data) -> int:
gameScoreB = 0
stoneRow = [int(stone) for stone in data[0].split(' ')]
for i in range(75):
print ("Loop:" + str(i) + " = " + str(len(stoneRow)))
listStone=[]
for stone in stoneRow:
try:
newStone = lookup[stone]
except:
newStone= laserStone(stone)
lookup[stone] = newStone
listStone = listStone + newStone
stoneRow = listStone
gameScoreB = len(stoneRow)
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()