82 lines
2.2 KiB
Python
82 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 *
|
|
|
|
setSampleMode(False)
|
|
|
|
aocDay = identifyDay(sys.argv[0])
|
|
aocYear = identifyYear(sys.argv[0])
|
|
|
|
path = getPath2Data(aocDay,aocYear)
|
|
filename = getFilename2Data(aocDay)
|
|
|
|
def getData(data):
|
|
calcs=[]
|
|
for dataLine in data:
|
|
calc=[]
|
|
result, values = dataLine.split(':')
|
|
calc.append(int(result))
|
|
calc.append([int(value) for value in values.strip().split()])
|
|
calcs.append(calc)
|
|
return calcs
|
|
|
|
def is_valid(result, values, part2):
|
|
if len(values) == 1:
|
|
return values[0]==result
|
|
if is_valid(result, [values[0]+values[1]] + values[2:], part2):
|
|
return True
|
|
if is_valid(result, [values[0]*values[1]] + values[2:], part2):
|
|
return True
|
|
if part2 and is_valid(result, [int(str(values[0])+str(values[1]))] + values[2:], part2):
|
|
return True
|
|
return False
|
|
|
|
def bruteforce_calc(calc,part2):
|
|
ret = 0
|
|
expectedResult = calc[0]
|
|
values = calc[1]
|
|
if is_valid(expectedResult, values, part2):
|
|
ret = expectedResult
|
|
return ret
|
|
|
|
def taskA (data) -> int:
|
|
gameScoreA = 0
|
|
calcs = getData(data)
|
|
for calc in calcs:
|
|
gameScoreA += bruteforce_calc(calc,False)
|
|
return gameScoreA
|
|
|
|
def taskB (data) -> int:
|
|
gameScoreB = 0
|
|
calcs = getData(data)
|
|
for calc in calcs:
|
|
gameScoreB += bruteforce_calc(calc,True)
|
|
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() |