diff --git a/2024/day08.py b/2024/day08.py index cae6b74..a5b6b34 100644 --- a/2024/day08.py +++ b/2024/day08.py @@ -5,6 +5,7 @@ import sys,time from helpingFunctions import * +from collections import defaultdict setSampleMode(False) @@ -15,18 +16,15 @@ path = getPath2Data(aocDay,aocYear) filename = getFilename2Data(aocDay) def identifyAntennes(data): - posAntennes={} + 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 != '.': - try: - posAntennes[dataValue].append((posX,posY)) - except: - posAntennes[dataValue] = [(posX,posY)] - return maxY,maxX,posAntennes + 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] @@ -43,30 +41,45 @@ def calcAntiNodes(posAntA, posAntB,maxPos): return (CX,CY) return (-1,-1) -def findAntiNodes(maxY,maxX,posAntennes): - maxPos = (maxX,maxY) - posAntiNodes=[] - posAntennesList = posAntennes.keys() - for antenne in posAntennesList: - for posAntA in posAntennes[antenne]: - for posAntB in posAntennes[antenne]: - if posAntA != posAntB: - antiNode = calcAntiNodes(posAntA,posAntB,maxPos) +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.append(antiNode) + 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 - maxY,maxX,posAntennes = identifyAntennes(data) - antiNodes = findAntiNodes(maxY,maxX,posAntennes) - antiNodes = list(set(antiNodes)) + maxPos,posAntennes = identifyAntennes(data) + antiNodes = findAntiNodesA(maxPos,posAntennes) gameScoreA = len(antiNodes) - print (data) 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: diff --git a/2024/day08/day08p1.py b/2024/day08/day08p1.py index b285750..282316e 100644 --- a/2024/day08/day08p1.py +++ b/2024/day08/day08p1.py @@ -38,9 +38,9 @@ for r in range(R): # To check if (r,c) (r1,c1) (r2,c2) are all on a line, check if (r,c)-(r1,c1) has the same slope as (r,c)-(r2,c2) # dr1/dc1 == dr2/dc2 - if (d1==2*d2 or d1*2==d2) and 0<=r