# AOC Day Script Day XX # Date : 20XX.12.XX # Python Code # Developer : David Bandeira import sys,time from helpingFunctions import * from collections import defaultdict setSampleMode(False) aocDay = identifyDay(sys.argv[0]) aocYear = identifyYear(sys.argv[0]) path = getPath2Data(aocDay,aocYear) filename = getFilename2Data(aocDay) def identifyAntennes(data): posAntennes=defaultdict(list) maxY = len(data) maxX = len(data[0]) for posY in range(maxY): for posX in range(maxX): dataValue = data[posY][posX] if dataValue != '.': posAntennes[dataValue].append((posX,posY)) return (maxX,maxY),posAntennes def isinGrid(pos,maxPos): return 0 <= pos[0] < maxPos[0] and 0 <= pos[1] < maxPos[1] def calcAntiNodes(posAntA, posAntB,maxPos): X1 = posAntA[0] X2 = posAntB[0] Y1 = posAntA[1] Y2 = posAntB[1] X3 = X1 + (X1-X2) Y3 = Y1 + (Y1-Y2) if isinGrid ((X3,Y3),maxPos): return (X3,Y3) return (-1,-1) def findAntiNodesA(maxPos,posAntennes): posAntiNodes=set() for keys, antenne in posAntennes.items(): for (X1,Y1) in antenne: for (X2,Y2) in antenne: if (X1,Y1) != (X2,Y2): antiNode = calcAntiNodes((X1,Y1),(X2,Y2),maxPos) if antiNode != (-1,-1): posAntiNodes.add(antiNode) return posAntiNodes def bruteForceAntiNodes(maxPos,posAntennes): posAntiNodes=set() for Y in range(maxPos[1]): for X in range(maxPos[0]): for keys, antenne in posAntennes.items(): for (X1,Y1) in antenne: for (X2,Y2) in antenne: if (X1,Y1) != (X2,Y2): dX1 = X - X1 dX2 = X - X2 dY1 = Y - Y1 dY2 = Y - Y2 if (dX1*dY2 == dX2*dY1): posAntiNodes.add((X,Y)) return posAntiNodes def taskA (data) -> int: gameScoreA = 0 maxPos,posAntennes = identifyAntennes(data) antiNodes = findAntiNodesA(maxPos,posAntennes) gameScoreA = len(antiNodes) return gameScoreA def taskB (data) -> int: gameScoreB = 0 maxPos,posAntennes = identifyAntennes(data) antiNodes = bruteForceAntiNodes(maxPos,posAntennes) gameScoreB = len(antiNodes) 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()