87 lines
2.8 KiB
Python
87 lines
2.8 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 turnLight(data,mode: int):
|
|
score=0
|
|
dataCommands = []
|
|
board = {}
|
|
|
|
for dataRow in data:
|
|
dataCommands.append(dataRow.replace('turn ','').replace(' through ',' ').replace(' ',','))
|
|
for dataCommand in dataCommands:
|
|
command,startCoordX,startCoordY,endCoordX,endCoordY = dataCommand.split(',')
|
|
for i in range(int(startCoordX),int(endCoordX)+1):
|
|
for j in range(int(startCoordY), int(endCoordY)+1):
|
|
coordXY = (i,j)
|
|
if mode == 0:
|
|
if command == 'on':
|
|
board[coordXY]=1
|
|
elif command == 'off':
|
|
board[coordXY]=0
|
|
elif command == 'toggle':
|
|
try:
|
|
board[coordXY]=board[coordXY] ^ 1
|
|
except:
|
|
board[coordXY] = 1
|
|
else:
|
|
if command == 'on':
|
|
try:
|
|
board[coordXY]+=1
|
|
except:
|
|
board[coordXY]=1
|
|
elif command == 'off':
|
|
try:
|
|
if board[coordXY] > 0 : board[coordXY]-=1
|
|
except:
|
|
board[coordXY]=0
|
|
elif command == 'toggle':
|
|
try:
|
|
board[coordXY]+=2
|
|
except:
|
|
board[coordXY]=2
|
|
for key,scoreValue in board.items():
|
|
score += scoreValue
|
|
return score
|
|
|
|
def taskA (data) -> int:
|
|
return turnLight(data,0)
|
|
|
|
def taskB (data) -> int:
|
|
return turnLight(data,1)
|
|
|
|
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() |