def limitlessSelfAvoid3D(): def filterNeighboursVisitable(neighs): return list(set(neighs) - visited) coord = (0, 0, 0) steps = 0 visited = set() while True: # print(coord, steps, visited) steps += 1 visited.add(coord) neighs = filterNeighboursVisitable(neighbours(coord, POINTS3D)) if neighs: return steps coord = oneOf(neighs)
def selfAvoidHappened(n): "Tells us if self avoidance happened during the trial for n*n matrix" visited = [[False] * n for j in range(n)] def isVisited(coord): return visited[coord[0]][coord[1]] def setVisited(coord): visited[coord[0]][coord[1]] = True def isOutside(coord): return not (0 <= coord[0] <= (n - 1) and 0 <= coord[1] <= (n - 1)) def isDeadEnd(coord): neighs = neighbours(coord) for point in neighs: if isOutside(point) or not isVisited(point): return False return True def filterNeighboursVisitableOrOutside(neighs): res = [] for point in neighs: if isOutside(point) or not isVisited(point): res += [point] return res coord = (n // 2, n // 2) steps = 0 box = (coord, coord) while True: steps += 1 neighs = filterNeighboursVisitableOrOutside(neighbours(coord)) if isDeadEnd(coord): return (False, steps, calcArea(box)) else: coord = oneOf(neighs) box = boundingRectangle(box, coord) if isOutside(coord): return (True, steps, calcArea(box)) setVisited(coord)
def torus_neighbours(coord, dim): neighs = neighbours(coord, POINTS8) res = [] for neigh in neighs: res += [force_lesser_than(neigh, dim)] return res
def greatestNeighbour(matr, coord): coords = neighbours(coord) neighs_vals = valsAt(matr, coords) return max(neighs_vals)
def nb_valid_neigbours(coord): return sum( [valid(neigh) for neigh in neighbours(coord, points=POINTS3D)])
def nb_false_neighbours(coord): return sum([ get(neigh) == False for neigh in neighbours(coord, points=POINTS3D) ])
def any_true_neigh(coord): neighs = neighbours(coord, points=POINTS3D) for neigh in neighs: if get(neigh) == True: return True return None
def isDeadEnd(coord): neighs = neighbours(coord) for point in neighs: if isOutside(point) or not isVisited(point): return False return True
def get_neighs(coord, width): POINTS=((0,width),(0,-width),(width,0),(-width,0)) return neighbours(coord, POINTS)