day 2024.08
This commit is contained in:
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user