day 8, refactor using grid lib
This commit is contained in:
parent
422e5440ff
commit
788c45edb7
24
d08/run.py
24
d08/run.py
|
@ -1,27 +1,13 @@
|
||||||
import sys, os
|
import sys, os
|
||||||
from collections import defaultdict as dd
|
from collections import defaultdict as dd
|
||||||
from itertools import combinations as comb
|
from itertools import combinations as comb
|
||||||
|
from grid import *
|
||||||
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]
|
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 sys.stdin.read().strip().split("\n")]
|
||||||
G = [list(l) for l in L]
|
|
||||||
|
|
||||||
|
# dict of antenna's position grouped by freq
|
||||||
# dico of position
|
|
||||||
P = dd(list)
|
P = dd(list)
|
||||||
W, H = grid_geom(G)
|
W, H = grid_geom(G)
|
||||||
for y in range(H): # rows
|
for y in range(H): # rows
|
||||||
|
@ -31,8 +17,8 @@ for y in range(H): # rows
|
||||||
P[v].append((x, y))
|
P[v].append((x, y))
|
||||||
|
|
||||||
A = set() # antinodes
|
A = set() # antinodes
|
||||||
for k,v in P.items():
|
for _,v in P.items():
|
||||||
for a,b in comb(v,2):
|
for a,b in comb(v,2): # for each pair of antenna resonating
|
||||||
x1, y1 = a
|
x1, y1 = a
|
||||||
x2, y2 = b
|
x2, y2 = b
|
||||||
dx = x2 - x1
|
dx = x2 - x1
|
||||||
|
|
Loading…
Reference in New Issue