day 8, easy

This commit is contained in:
setop 2024-12-08 11:07:57 +01:00
parent 8b8d0ee58b
commit 3fe4944d79
1 changed files with 49 additions and 0 deletions

49
d08/run.py Normal file
View File

@ -0,0 +1,49 @@
import sys, os
from collections import defaultdict as dd
from itertools import combinations as comb
def grid_geom(G):
H = len(G)
W = len(G[0])
return W, H
def in_grid(G, x, y) -> bool:
W, H = grid_geom(G)
return x>=0 and x<W and y>=0 and y<H
def get_in_grid(G, x, y):
if in_grid(G, x,y):
return G[y][x]
R = list(range(50)) if len(sys.argv)>1 and sys.argv[1]=="2" else [1]
L = sys.stdin.read().strip().split("\n")
G = [list(l) for l in L]
# dico of position
P = dd(list)
W, H = grid_geom(G)
for y in range(H): # rows
for x in range(W): # cols
v = get_in_grid(G, x, y)
if v != '.':
P[v].append((x, y))
A = set() # antinodes
for k,v in P.items():
for a,b in comb(v,2):
x1, y1 = a
x2, y2 = b
dx = x2 - x1
dy = y2 - y1
for i in R:
x3 = x1 - i*dx
y3 = y1 - i*dy
x4 = x2 + i*dx
y4 = y2 + i*dy
if in_grid(G, x3, y3):
A.add((x3,y3))
if in_grid(G, x4, y4):
A.add((x4,y4))
print(len(A))