If you enjoy solving puzzles, the NSA publishes a monthly puzzle. Since the NSA employs some of the best maths minds in the world, some of these puzzles can be quite entertaining.

The January 2017 puzzle is here.

The puzzle basically asks if we are looking for a hidden easter egg in a grid, does it matter which route we take. So, for example, suppose we are looking for treasure in Manhattan, are we likely to find it sooner if we move North/South then East/West or East/West then North/South. Intuition tells us that it makes no difference.

Spoiler ahead

As my daughter pointed out, one strategy must be faster than the other or they wouldn’t have asked the question.

Well, yes, it does make a difference which direction you travel. But, in order to convince myself, I wrote a small program to simulate moving in the different directions. The results were convincing. Here is the program

"""
A program to simulate the puzzle presented here:

    https://www.nsa.gov/news-features/puzzles-activities/puzzle-periodical/2017/puzzle-periodical-01.shtml
"""

import random

bob = {lhs:rhs for lhs, rhs in zip("ABCDEFGHIJKL", range(12))}
alice = {lhs:rhs for lhs, rhs in zip("AEIBFJCGKDHL", range(12))}


def trial():
    eggs = random.sample("ABCDEFGHIJKL", 2)
    alice_score = min([alice[e] for e in eggs])
    bob_score = min([bob[e] for e in eggs])
    if alice_score < bob_score:
        return 1
    elif bob_score < alice_score:
        return - 1
    return 0

def simulation():
    alice_tot = 0
    bob_tot = 0
    for i in range(10000):
        results = trial()
        if results > 0:
            alice_tot += 1
        elif results < 0:
            bob_tot += 1

    return alice_tot, bob_tot

for i in range(10):
    alice_sum, bob_sum = simulation()
    print 'Simulation {}: {} {}'.format(i, alice_sum, bob_sum)
      

Running the program gives us the following results:

Simulation 0: 3949 4101
Simulation 1: 3882 4111
Simulation 2: 3988 4062
Simulation 3: 3884 4141
Simulation 4: 3964 4067
Simulation 5: 4027 4005
Simulation 6: 3875 4118
Simulation 7: 3998 4023
Simulation 8: 3880 4136
Simulation 9: 3901 4106

Fairly convincing evidence that Bob’s strategy is better than Alice’s.