65 lines
1.6 KiB
Python
65 lines
1.6 KiB
Python
# AOC Day Script Day XX
|
|
# Date : 20XX.12.XX
|
|
# Python Code
|
|
# Developer : David Bandeira
|
|
|
|
import sys,time,re
|
|
from helpingFunctions import *
|
|
import json
|
|
|
|
setSampleMode(False)
|
|
|
|
aocDay = identifyDay(sys.argv[0])
|
|
aocYear = identifyYear(sys.argv[0])
|
|
|
|
path = getPath2Data(aocDay,aocYear)
|
|
filename = getFilename2Data(aocDay)
|
|
|
|
def n(json):
|
|
if type(json) == int:
|
|
return json
|
|
if type(json) == list:
|
|
return sum([n(json) for json in json])
|
|
if type(json) != dict:
|
|
return 0
|
|
if 'red' in json.values():
|
|
return 0
|
|
return n(list(json.values()))
|
|
|
|
def taskA (data) -> int:
|
|
gameScoreA = 0
|
|
for dataLine in data:
|
|
dataValues = re.findall('(-?\d+)',dataLine)
|
|
for dataValue in dataValues:
|
|
gameScoreA = gameScoreA+ int(dataValue)
|
|
return gameScoreA
|
|
|
|
def taskB (data) -> int:
|
|
gameScoreB = 0
|
|
for dataLine in data:
|
|
gameScoreB = gameScoreB+ n(json.loads(dataLine))
|
|
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() |