a puzzle about domestic boundaries

Another week, another riddler. The problem is:

    Consider four square-shaped ranches, arranged in a two-by-two pattern, as if part of a larger checkerboard. One family lives on each ranch, and each family builds a small house independently at a random place within the property. Later, as the families in adjacent quadrants become acquainted, they construct straight-line paths between the houses that go across the boundaries between the ranches, four in total. These paths form a quadrilateral circuit path connecting all four houses. This circuit path is also the boundary of the area where the families’ children are allowed to roam.

    What is the probability that the children are able to travel in a straight line from any allowed place to any other allowed place without leaving the boundaries? (In other words, what is the probability that the quadrilateral is convex?)


I wish I could say I did something cool. Instead I just brute forced it. After ten million iterations the resulting probability of a convex ranch is 9.09%

import random
import matplotlib.pyplot as plt
from matplotlib.path import Path
import matplotlib.patches as patches

def dot_prod(A,B,C): #Ax,Ay,Bx,By,Cx,Cy):
    #Find the vector components
    BAx = B[0] - A[0]   #Bx-Ax
    BAy = B[1] - A[1]   #By-Ay
    BCx = C[0] - B[0]   #Cx-Bx
    BCy = C[1] - B[1]   #Cy-By

    #Find cross prod
    return BAx*BCy-BAy*BCx

def draw_ranch(coords,iteration,fail):
    codes = [Path.MOVETO,

    verts = [
        (coords[0][0], coords[0][1]), # left, bottom
        (coords[3][0], coords[3][1]), # left, top
        (coords[2][0], coords[2][1]), # right, top
        (coords[1][0], coords[1][1]), # right, bottom
        (0., 0.), # ignored

    path = Path(verts, codes)

    fig = plt.figure()
    plt.axhline(y=1, xmin=0, xmax=2, linewidth=1, color = 'k', linestyle='dashed')
    plt.axvline(x=1, ymin=0, ymax=2, linewidth=1, color = 'k', linestyle='dashed')
    ax = fig.add_subplot(111)
    if fail:
        patch = patches.PathPatch(path, ec='r', facecolor='none', lw=1)
        patch = patches.PathPatch(path, ec='g', facecolor='none', lw=1)
    xs, ys = zip(*verts[0:4])
    ax.plot(xs, ys, 'x', lw=2, color='black', ms=10)


def check_convexity(coordinates,iteration):
    got_neg = False
    got_paws = False
    num_families = len(coordinates)
    for i in range(0,num_families):
        sign = dot_prod(coordinates[i],coordinates[(i+1)%num_families],coordinates[(i+2)%num_families])
        if sign > 0:
            got_paws = True
            got_neg = True

        #Draw and show data if you want
        #if (got_paws & got_neg):
            #print(coord1," ",coord2," ",coord3)
            #print(sign," ",got_paws," ",got_neg)
        #draw_ranch(coordinates,iteration,(got_paws & got_neg))
    return got_paws & got_neg #Returns false if they were all the same sign

def run_tests(iterations):
    num_convex = 0
    total = 0
    for j in range(0,iterations):
        coords = [[random.random(),random.random()],[random.random()+1,random.random()],[random.random()+1,random.random()+1],[random.random(),random.random()+1]]
        num_convex = check_convexity(coords,j) + num_convex
        total = total + 1
    return float(num_convex/total)


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s