Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. import time
  2. start_time = time.time()
  3.  
  4. data = 2568
  5.  
  6. def do_calc(x, y, n):
  7. val = (x+10)*((x+10)*y + n)
  8. t = val%1000
  9. t //= 100
  10. return t-5
  11.  
  12. print(do_calc(3,5,8))
  13. print(do_calc(122,79,57))
  14. print(do_calc(217,196,39))
  15. print(do_calc(101,153,71))
  16.  
  17. grid_size = 300
  18.  
  19. import numpy
  20.  
  21. grid = numpy.zeros((grid_size, grid_size))
  22. coords = numpy.arange(grid_size)+1
  23.  
  24. x_coords = numpy.zeros((grid_size, grid_size))
  25. x_coords[:] = coords
  26.  
  27. y_coords = numpy.zeros((grid_size, grid_size))
  28. y_coords[:] = coords[:,None]
  29.  
  30.  
  31. n = 2568
  32.  
  33. t = ((x_coords+10)*y_coords + n)%1000
  34. t = (t*(x_coords+10))%1000
  35. t = t//100 - 5
  36.  
  37. v = t[:grid_size - 2] + t[1:grid_size - 1] + t[2:grid_size]
  38. v = v[:,:grid_size-2] + v[:,1:grid_size-1] + v[:,2:grid_size]
  39.  
  40. res = numpy.unravel_index(numpy.argmax(v.T), v.shape)
  41.  
  42. th = t
  43. tv = numpy.concatenate((numpy.zeros((1, grid_size)),t[:-1]))
  44. horiz_compress = th
  45. vert_compress = numpy.zeros((grid_size, grid_size))
  46. square_maxs = numpy.zeros((grid_size+1, grid_size+1))
  47.  
  48. max_power = 0
  49. max_coord = None
  50. max_square_size = None
  51. for i in range(300):
  52. square_maxs = square_maxs[:-1,:-1] + horiz_compress + vert_compress
  53. th = th[1:,1:]
  54. horiz_compress = horiz_compress[1:,:-1]+th
  55. tv = tv[1:,1:]
  56. vert_compress = vert_compress[:-1,1:] + tv
  57. res = numpy.unravel_index(numpy.argmax(square_maxs.T), square_maxs.shape)
  58. res_shifted = tuple(x+1 for x in res)
  59. power = square_maxs.T[res]
  60. if power >= max_power:
  61. max_power = power
  62. max_square_size = i+1
  63. max_coord = res_shifted
  64.  
  65. print("{},{},{}".format(max_coord[0], max_coord[1],max_square_size))
  66. print("max power attained:", max_power)
  67. print("Time elapsed: {:.2f}s".format(time.time() - start_time))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement