import math def coordsToListIndex(coords, a=0, b=0): x = coords[0]+a #2 y = coords[1]+b #-2 ceiled_sqrt_of_smallest_square_greater_than = max(abs(x),abs(y))*2+1 #5 smallest_square_greater_than = ceiled_sqrt_of_smallest_square_greater_than**2 #25 value_with_max_mag = x if max(abs(x),abs(y))==abs(x) and x!=-y else y if value_with_max_mag == y: if y < 0: number_of_square_sides_covered = 0 sign_of_distance_from_nearest_square_side_midpoint = -1; else: number_of_square_sides_covered = 2 sign_of_distance_from_nearest_square_side_midpoint = 1 else: if x < 0: number_of_square_sides_covered = 1 sign_of_distance_from_nearest_square_side_midpoint = 1 else: number_of_square_sides_covered = 3 sign_of_distance_from_nearest_square_side_midpoint = -1 distance_from_nearest_following_vertex_of_square_ring = sign_of_distance_from_nearest_square_side_midpoint * (x if value_with_max_mag==y else y) + ((ceiled_sqrt_of_smallest_square_greater_than+1)//2 - 1) distance_on_square_ring_away_from_smallest_square_greater_than = number_of_square_sides_covered * (ceiled_sqrt_of_smallest_square_greater_than-1) + distance_from_nearest_following_vertex_of_square_ring i = smallest_square_greater_than - distance_on_square_ring_away_from_smallest_square_greater_than return i def listIndexToCoords(i): if i==1: return (0,0) ceiled_sqrt_of_smallest_square_greater_than = math.ceil(math.sqrt(i)); if ceiled_sqrt_of_smallest_square_greater_than%2==0: ceiled_sqrt_of_smallest_square_greater_than+=1 smallest_square_greater_than = ceiled_sqrt_of_smallest_square_greater_than**2 distance_on_square_ring_away_from_smallest_square_greater_than = smallest_square_greater_than - i; number_of_square_sides_covered = distance_on_square_ring_away_from_smallest_square_greater_than // (ceiled_sqrt_of_smallest_square_greater_than-1) distance_from_nearest_following_vertex_of_square_ring = distance_on_square_ring_away_from_smallest_square_greater_than % (ceiled_sqrt_of_smallest_square_greater_than-1) distance_from_nearest_square_side_midpoint = distance_from_nearest_following_vertex_of_square_ring - ((ceiled_sqrt_of_smallest_square_greater_than+1)//2 - 1) if number_of_square_sides_covered==0: x = -distance_from_nearest_square_side_midpoint y = -(ceiled_sqrt_of_smallest_square_greater_than-1)//2 elif number_of_square_sides_covered==1: x = -(ceiled_sqrt_of_smallest_square_greater_than-1)//2 y = distance_from_nearest_square_side_midpoint elif number_of_square_sides_covered==2: x = distance_from_nearest_square_side_midpoint y = (ceiled_sqrt_of_smallest_square_greater_than-1)//2 else: #number_of_square_sides_covered==3 x = (ceiled_sqrt_of_smallest_square_greater_than-1)//2 y = -distance_from_nearest_square_side_midpoint return (x,y) def my_answer(bound): i = 2 if bound==1: return 1 mylist = [None,1] coords = (0,0) adjacent_list_indices = [] while (i < bound): coords = listIndexToCoords(i) adjacent_list_indices = [coordsToListIndex(coords, 0, 1), coordsToListIndex(coords, 1, 0), coordsToListIndex(coords, 1, 1), coordsToListIndex(coords, 0, -1), coordsToListIndex(coords, -1, 0), coordsToListIndex(coords, -1, -1), coordsToListIndex(coords, -1, 1), coordsToListIndex(coords, 1, -1)] mylist.append(sum(map(lambda x: mylist[x] if len(mylist)>x else 0, adjacent_list_indices))) if mylist[i] > bound: return mylist[i] else: i += 1