Advertisement
Guest User

Untitled

a guest
Dec 5th, 2017
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.48 KB | None | 0 0
  1. def get_n_elems(rings):
  2.     return (2*rings - 1) ** 2
  3.  
  4. def make_coords(n_elems):
  5.     coords = [[0, 0]]
  6.     direction = "right"
  7.     for i in range(1, n_elems):
  8.         if direction == "right":
  9.             row = coords[i-1][0]
  10.             col = coords[i-1][1] + 1
  11.             if row + 1 == col:
  12.                 # switch walking direction from right to up in these
  13.                 # "lower right" corners of the grid
  14.                 direction = "up"
  15.                
  16.         elif direction == "up":
  17.             row = coords[i-1][0] - 1
  18.             col = coords[i-1][1]
  19.             if -row == col:
  20.                 direction = "left"
  21.                
  22.         elif direction == "left":
  23.             row = coords[i-1][0]
  24.             col = coords[i-1][1] - 1
  25.             if row == col:
  26.                 direction = "down"
  27.                    
  28.         elif direction == "down":
  29.             row = coords[i-1][0] + 1
  30.             col = coords[i-1][1]
  31.             if -row == col:
  32.                 direction = "right"
  33.                
  34.         coords.append([row, col])
  35.     return coords
  36.  
  37. def get_neighbor_coords(coord, rings):
  38.     return [
  39.         [coord[0]+dx, coord[1]+dy]
  40.         for dx in range(-1, 2)
  41.         for dy in range(-1, 2)
  42.         if not (dx == 0 and dy == 0)  # so that the field is not its own neighbor
  43.         # here we check that the fields are not outside of the outermost ring:
  44.         and (not abs(coord[0]+dx) >= rings)
  45.         and (not abs(coord[1]+dy) >= rings)
  46.     ]
  47.  
  48. def get_elem_by_coord(chain, coords, coord):
  49.     for i in range(len(coords)):
  50.         if coords[i]  == coord:
  51.             return chain[i]
  52.  
  53. def fill_chain(rings):
  54.     n_elems = get_n_elems(rings)
  55.     chain = [0] * n_elems
  56.     chain[0] = 1
  57.     coords = make_coords(n_elems)
  58.     for i in range(1, n_elems):
  59.         new_elem = fill_this_chain_elem(coords[i], coords, chain)
  60.         print("Filling chain at " + str(i) + " with " + str(new_elem))
  61.         chain[i] = new_elem
  62.     return chain
  63.  
  64. def fill_this_chain_elem(coord, coords, chain):
  65.     neighbors = get_neighbor_coords(coord, rings)
  66.     return sum(get_elem_by_coord(chain, coords, neighbor_coord)
  67.                for neighbor_coord in neighbors)
  68.        
  69. def get_first_greater_than(chain, number):
  70.     for elem in chain:
  71.         if elem > number:
  72.             return elem
  73.                            
  74. if __name__ == "__main__":
  75.     rings = 5
  76.     filled_chain = fill_chain(rings)
  77.     print(get_first_greater_than(filled_chain, 368078))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement