day 2024.08
This commit is contained in:
@@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
import sys,time
|
import sys,time
|
||||||
from helpingFunctions import *
|
from helpingFunctions import *
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
setSampleMode(False)
|
setSampleMode(False)
|
||||||
|
|
||||||
@@ -15,18 +16,15 @@ path = getPath2Data(aocDay,aocYear)
|
|||||||
filename = getFilename2Data(aocDay)
|
filename = getFilename2Data(aocDay)
|
||||||
|
|
||||||
def identifyAntennes(data):
|
def identifyAntennes(data):
|
||||||
posAntennes={}
|
posAntennes=defaultdict(list)
|
||||||
maxY = len(data)
|
maxY = len(data)
|
||||||
maxX = len(data[0])
|
maxX = len(data[0])
|
||||||
for posY in range(maxY):
|
for posY in range(maxY):
|
||||||
for posX in range(maxX):
|
for posX in range(maxX):
|
||||||
dataValue = data[posY][posX]
|
dataValue = data[posY][posX]
|
||||||
if dataValue != '.':
|
if dataValue != '.':
|
||||||
try:
|
|
||||||
posAntennes[dataValue].append((posX,posY))
|
posAntennes[dataValue].append((posX,posY))
|
||||||
except:
|
return (maxX,maxY),posAntennes
|
||||||
posAntennes[dataValue] = [(posX,posY)]
|
|
||||||
return maxY,maxX,posAntennes
|
|
||||||
|
|
||||||
def isinGrid(pos,maxPos):
|
def isinGrid(pos,maxPos):
|
||||||
return 0 <= pos[0] < maxPos[0] and 0 <= pos[1] < maxPos[1]
|
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 (CX,CY)
|
||||||
return (-1,-1)
|
return (-1,-1)
|
||||||
|
|
||||||
def findAntiNodes(maxY,maxX,posAntennes):
|
def findAntiNodesA(maxPos,posAntennes):
|
||||||
maxPos = (maxX,maxY)
|
posAntiNodes=set()
|
||||||
posAntiNodes=[]
|
for keys, antenne in posAntennes.items():
|
||||||
posAntennesList = posAntennes.keys()
|
for (X1,Y1) in antenne:
|
||||||
for antenne in posAntennesList:
|
for (X2,Y2) in antenne:
|
||||||
for posAntA in posAntennes[antenne]:
|
if (X1,Y1) != (X2,Y2):
|
||||||
for posAntB in posAntennes[antenne]:
|
antiNode = calcAntiNodes((X1,Y1),(X2,Y2),maxPos)
|
||||||
if posAntA != posAntB:
|
|
||||||
antiNode = calcAntiNodes(posAntA,posAntB,maxPos)
|
|
||||||
if antiNode != (-1,-1):
|
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
|
return posAntiNodes
|
||||||
|
|
||||||
def taskA (data) -> int:
|
def taskA (data) -> int:
|
||||||
gameScoreA = 0
|
gameScoreA = 0
|
||||||
maxY,maxX,posAntennes = identifyAntennes(data)
|
maxPos,posAntennes = identifyAntennes(data)
|
||||||
antiNodes = findAntiNodes(maxY,maxX,posAntennes)
|
antiNodes = findAntiNodesA(maxPos,posAntennes)
|
||||||
antiNodes = list(set(antiNodes))
|
|
||||||
gameScoreA = len(antiNodes)
|
gameScoreA = len(antiNodes)
|
||||||
print (data)
|
|
||||||
return gameScoreA
|
return gameScoreA
|
||||||
|
|
||||||
def taskB (data) -> int:
|
def taskB (data) -> int:
|
||||||
gameScoreB = 0
|
gameScoreB = 0
|
||||||
|
maxPos,posAntennes = identifyAntennes(data)
|
||||||
|
antiNodes = bruteForceAntiNodes(maxPos,posAntennes)
|
||||||
|
gameScoreB = len(antiNodes)
|
||||||
return gameScoreB
|
return gameScoreB
|
||||||
|
|
||||||
def task(task: int,data) -> int:
|
def task(task: int,data) -> int:
|
||||||
|
|||||||
@@ -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)
|
# 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
|
# dr1/dc1 == dr2/dc2
|
||||||
|
|
||||||
if (d1==2*d2 or d1*2==d2) and 0<=r<R and 0<=c<C and (dr1*dc2 == dc1*dr2):
|
if (d1==2*d2 or d1*2==d2) and (dr1*dc2 == dc1*dr2):
|
||||||
A1.add((r,c))
|
A1.add((r,c))
|
||||||
if 0<=r<R and 0<=c<C and (dr1*dc2 == dc1*dr2):
|
if (dr1*dc2 == dc1*dr2):
|
||||||
#print(f'{r4=} {c=} {r1=} {c1=} {r2=} {c2=} {k=} {dr1=} {dr2=} {dc1=} {dc2=}')
|
#print(f'{r4=} {c=} {r1=} {c1=} {r2=} {c2=} {k=} {dr1=} {dr2=} {dc1=} {dc2=}')
|
||||||
A2.add((r,c))
|
A2.add((r,c))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user