From 70694d6e071babfff0e8d5a280cc1bab4204c5f0 Mon Sep 17 00:00:00 2001 From: David Bandeira Date: Sun, 8 Dec 2024 18:30:56 +0100 Subject: [PATCH] day 2024.08 --- 2024/day07/day07a.py | 2 +- 2024/day08.py | 47 +++++++++++++++++++++++++++++++++++++ 2024/day08/day08p1.py | 51 +++++++++++++++++++++++++++++++++++++++++ 2024/day08/input.day08 | 50 ++++++++++++++++++++++++++++++++++++++++ 2024/day08/sample.day08 | 12 ++++++++++ 5 files changed, 161 insertions(+), 1 deletion(-) create mode 100644 2024/day08/day08p1.py diff --git a/2024/day07/day07a.py b/2024/day07/day07a.py index 6ab1dfb..cac642e 100644 --- a/2024/day07/day07a.py +++ b/2024/day07/day07a.py @@ -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() diff --git a/2024/day08.py b/2024/day08.py index e57c061..cae6b74 100644 --- a/2024/day08.py +++ b/2024/day08.py @@ -13,9 +13,56 @@ 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: diff --git a/2024/day08/day08p1.py b/2024/day08/day08p1.py new file mode 100644 index 0000000..b285750 --- /dev/null +++ b/2024/day08/day08p1.py @@ -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