import math


def part_1(memory_loc):
    """Get the manhattan distance from a location to the centre of a
    (1-indexed) spiral array

    Arguments
    ---------
    memory_loc : int, the location of the starting point. Locations are
        numbered anticlockwise from the centre, and head right first, e.g.
        5 4 3
        6 1 2
        7 8 9

    Returns
    -------
    nr_steps : int, The manhattan distance from memory_loc to '1'
        i.e. the centre
    """
    if memory_loc == 1:
        return 0
    else:
        layer_nr = 1
        while (2*layer_nr-1) ** 2 < memory_loc:
            layer_nr += 1
        layer_nr -= 1  # 0-indexed layer number
        square_nr = (2 * (layer_nr+1) - 1) ** 2
        square_nr_prev = (2 * (layer_nr) - 1) ** 2
        perim_loc = memory_loc - square_nr_prev
        perim_len = square_nr - square_nr_prev
        side_len = perim_len / 4
        assert side_len == int(side_len)
        side_len = int(side_len)
        side_loc = perim_loc % side_len
        nr_steps = layer_nr + abs(side_loc - math.floor(side_len / 2))
        return int(nr_steps)