How to cheat at Mensa puzzles

At freshers’ fayre (yeah, a long time ago now) I was handed a slip from Mensa. I intended to follow it up later but being busy I never got round to it. Today I rediscovered it on my desk, and of course it had a couple of puzzles on it. One is a pretty simple number sequence. The other has you find an anagram:

Rearrange the letters of ‘ANY TIME‘ to give a seven letter word. What is it?

Obviously “anytime” is excluded (well, we’re not Americans ;). I tried to find one manually for a while, but then realised I could just solve it with a couple of commands:

[pwb@rhuidean ~]$ ghci -e 'Monad.mapM_ putStrLn (let s = "anytime" in filter (\s2 -> all (`elem` s2) s) $ Control.Monad.replicateM 7 s)' > words
[pwb@rhuidean ~]$ grep -f words /usr/share/dict/words

This was taking a long time. But then I realised you can cut out a big chunk of the search space by filtering out all the non-anagrams, which you can do easily with grep.

[pwb@rhuidean ~]$ grep '^[anytime]{7}$' /usr/share/dict/words | grep -f words
amenity
anytime

I wonder how this reflects on my intelligence… :)

Leave a Reply