day 2024.08

This commit is contained in:
2024-12-08 18:30:56 +01:00
parent da16807457
commit 70694d6e07
5 changed files with 161 additions and 1 deletions

View File

@@ -7,7 +7,7 @@ def pr(s):
# pc.copy(s)
sys.setrecursionlimit(10**6)
infile = sys.argv[1] if len(sys.argv)>=2 else '7.in'
infile = '/home/bandeira/git.bandeira/aoc/2024/day07/sample.day07'
infile = '/home/bandeira/git.bandeira/aoc/2024/day07/input.day07'
p1 = 0
p2 = 0
D = open(infile).read().strip()

View File

@@ -14,8 +14,55 @@ aocYear = identifyYear(sys.argv[0])
path = getPath2Data(aocDay,aocYear)
filename = getFilename2Data(aocDay)
def identifyAntennes(data):
posAntennes={}
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
def isinGrid(pos,maxPos):
return 0 <= pos[0] < maxPos[0] and 0 <= pos[1] < maxPos[1]
def calcAntiNodes(posAntA, posAntB,maxPos):
AX = posAntA[0]
BX = posAntB[0]
AY = posAntA[1]
BY = posAntB[1]
CX = AX + (AX-BX)
CY = AY + (AY-BY)
if isinGrid ((CX,CY),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)
if antiNode != (-1,-1):
posAntiNodes.append(antiNode)
return posAntiNodes
def taskA (data) -> int:
gameScoreA = 0
maxY,maxX,posAntennes = identifyAntennes(data)
antiNodes = findAntiNodes(maxY,maxX,posAntennes)
antiNodes = list(set(antiNodes))
gameScoreA = len(antiNodes)
print (data)
return gameScoreA
def taskB (data) -> int:

51
2024/day08/day08p1.py Normal file
View File

@@ -0,0 +1,51 @@
import sys
import re
from collections import defaultdict, Counter, deque
#import pyperclip as pc
def pr(s):
print(s)
# pc.copy(s)
sys.setrecursionlimit(10**6)
infile = sys.argv[1] if len(sys.argv)>=2 else '/home/bandeira/git.bandeira/aoc/2024/day08/input.day08'
p1 = 0
p2 = 0
D = open(infile).read().strip()
G = D.split('\n')
R = len(G)
C = len(G[0])
P = defaultdict(list)
for r in range(R):
for c in range(C):
if G[r][c] != '.':
P[G[r][c]].append((r,c))
A1 = set()
A2 = set()
for r in range(R):
for c in range(C):
for k,vs in P.items():
for (r1,c1) in vs:
for (r2,c2) in vs:
if (r1,c1) != (r2,c2):
d1 = abs(r-r1)+abs(c-c1)
d2 = abs(r-r2)+abs(c-c2)
dr1 = r-r1
dr2 = r-r2
dc1 = c-c1
dc2 = c-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
if (d1==2*d2 or d1*2==d2) and 0<=r<R and 0<=c<C and (dr1*dc2 == dc1*dr2):
A1.add((r,c))
if 0<=r<R and 0<=c<C and (dr1*dc2 == dc1*dr2):
#print(f'{r4=} {c=} {r1=} {c1=} {r2=} {c2=} {k=} {dr1=} {dr2=} {dc1=} {dc2=}')
A2.add((r,c))
p1 = len(A1)
p2 = len(A2)
pr(p1)
pr(p2)

View File

@@ -0,0 +1,50 @@
..........M..........j.............y.....O........
...B...............q......m........lGO............
....................q......2.l.GQ...O.............
.....X.......................................4....
.....................q............................
....M......P...............xl.K.............2.....
....F.........L.......C.K..............m..........
..........FM......P....jy......m..........o...r...
..X.......P.....RL..............G..x..........4...
............L..........NC.....q...................
.....C.X...............K....y..........4..........
........S...R.............j.x.....V...4...........
.....................R..x.....V..i......m.........
...........................R.V......N.......X.....
.....F.........M......N......E....................
................v................T.......F......O.
.............................N...V.......Q........
...v.....................C.....i..................
......c.....W..n.w........................E.......
3...................c.....................Q..6....
...........h......................j...............
.......n.0......h.................E..............2
.v.............7.......120.....c..................
......n.0............w...........D.t.........E...r
....8..3......0.w.hP....z...D..T...............r..
.................f........T........G......eQ......
......f.n.....7..p................................
.....Y..7.......f......I......D......K............
............Uf....T..W.....D..r...i...............
......I...............................Z...........
....5....B.......b..............s..............Z..
..........d...W..Uwh.............c..........i.....
..I.3..Y......................e...................
.....p.b..........k......7........................
p...........k....I..b..........s..................
.....k.......o...........W........................
.A..Y..........U.................a........6.......
..A...Y.p...................................6.....
B......k..........................Z............u..
...3.....................s..............a.........
......A.........................g.....a...........
.......A....8...b.U......H....sS..................
.........................S1.............t.........
.....................9z..e.....5..1.g.u...........
.......................z....d....g....H.J....o.6..
........B................d.....u....9.J.H.........
.8........S.................u9.............J.....H
.....................Z5.............t1...........a
.....................e..v...................o..t..
.....8...............L.....z.............J........

View File

@@ -0,0 +1,12 @@
............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............