98 lines
3.6 KiB
Python
98 lines
3.6 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 calculateWire(defWire,calculationSteps,valueWire):
|
|
retVal = 0
|
|
try:
|
|
retVal = int(defWire)
|
|
return retVal,valueWire
|
|
except ValueError:
|
|
pass
|
|
|
|
if defWire not in valueWire:
|
|
calculationStep = calculationSteps[defWire]
|
|
if len(calculationStep) == 1:
|
|
calculateValueA, valueWire = calculateWire(calculationStep[0],calculationSteps,valueWire)
|
|
retVal = calculateValueA
|
|
else:
|
|
calc = calculationStep[-2]
|
|
if calc == 'AND':
|
|
calculateValueA, valueWire = calculateWire(calculationStep[0],calculationSteps,valueWire)
|
|
calculateValueB, valueWire = calculateWire(calculationStep[2],calculationSteps,valueWire)
|
|
retVal = calculateValueA & calculateValueB
|
|
elif calc == 'OR':
|
|
calculateValueA, valueWire = calculateWire(calculationStep[0],calculationSteps,valueWire)
|
|
calculateValueB, valueWire = calculateWire(calculationStep[2],calculationSteps,valueWire)
|
|
retVal = calculateValueA | calculateValueB
|
|
elif calc == 'NOT':
|
|
calculateValueA, valueWire = calculateWire(calculationStep[1],calculationSteps,valueWire)
|
|
retVal = 0xffff - calculateValueA
|
|
elif calc == 'RSHIFT':
|
|
calculateValueA, valueWire = calculateWire(calculationStep[0],calculationSteps,valueWire)
|
|
calculateValueB, valueWire = calculateWire(calculationStep[2],calculationSteps,valueWire)
|
|
retVal = calculateValueA >> calculateValueB
|
|
elif calc == 'LSHIFT':
|
|
calculateValueA, valueWire = calculateWire(calculationStep[0],calculationSteps,valueWire)
|
|
calculateValueB, valueWire = calculateWire(calculationStep[2],calculationSteps,valueWire)
|
|
retVal = calculateValueA << calculateValueB
|
|
valueWire[defWire] = retVal
|
|
return valueWire[defWire],valueWire
|
|
|
|
def createCalcSteps(data):
|
|
calculationSteps={}
|
|
for dataRow in data:
|
|
(commandString, targetWire) = dataRow.split('->')
|
|
calculationSteps[targetWire.strip()]= commandString.strip().split(' ')
|
|
return calculationSteps
|
|
|
|
def taskA (data) -> int:
|
|
gameScoreA = 0
|
|
calculationSteps=createCalcSteps(data)
|
|
gameScoreA,valueWire = calculateWire('a',calculationSteps,{})
|
|
return gameScoreA
|
|
|
|
def taskB (data,valueA) -> int:
|
|
gameScoreB = 0
|
|
calculationSteps=createCalcSteps(data)
|
|
gameScoreB,valueWire = calculateWire('a',calculationSteps,{'b':valueA})
|
|
return gameScoreB
|
|
|
|
def task(task: int,data) -> int:
|
|
global scoreA
|
|
global scoreB
|
|
score=0
|
|
if task == 1:
|
|
score = taskA(data)
|
|
scoreA=score
|
|
elif task == 2:
|
|
score = taskB(data,scoreA)
|
|
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() |