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