From 3fe4944d79a568dcba65c6d75b9669eb60004f9c Mon Sep 17 00:00:00 2001 From: setop Date: Sun, 8 Dec 2024 11:07:57 +0100 Subject: [PATCH] day 8, easy --- d08/run.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 d08/run.py diff --git a/d08/run.py b/d08/run.py new file mode 100644 index 0000000..361b731 --- /dev/null +++ b/d08/run.py @@ -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=0 and y1 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))