Enigmatic Code

Programming Enigma Puzzles

Enigma 343: In the mews

From New Scientist #1492, 23rd January 1986 [link]

Silent Mews, the cul-de-sac where I live, is dull and uniform. Every house on this side of the road has one exactly corresponding to it on the other side, and vice versa. The only exciting thing about it is that the houses are numbered boustrophedon. That is, starting at number 1 and visiting each house in numerical order takes you up one side of the street, along the end of the mews and back along the other side.

To lend my house individuality, I have bought brass numbers for my front door (all the other houses have plastic). Each digit costs the same, so it would cost 222 times what I paid for my door number to replace everyone else’s door numbers with brass. Not only does my property have a special number, but my number has a special property. Rearranging its digits produces exactly four numbers. One of these is my number and one is the number of another house in the mews.

If more houses were to be added on after the last house on the mews, it it would be possible for the set of digits in my number to be the only set which appeared in a total of exactly four house numbers, two of which were in the old part of the mews, and two which appeared in the newly extended part.

You may wonder whether, besides the houses on the two long sides of the mews, there are also houses at the blind end of the mews, but the answer to this should be obvious, as is the answer to this puzzle, namely: what is the number of the house directly opposite mine?

I am going to be away next week, but I will try and keep to the posting schedule if possible.


One response to “Enigma 343: In the mews

  1. Jim Randell 6 May 2016 at 7:55 am

    This Python program runs in 40ms.

    from itertools import count
    from collections import defaultdict
    from enigma import irange, join, Accumulator, printf
    # generate possible numbers of houses
    def generate():
      # count the digits
      t = 0
      for n in count(1):
        d = len(str(n))
        t += d
        # if my number has k digits then:
        # t - k = 222k, so t = 223k
        (k, r) = divmod(t, 223)
        if r == 0 and not(k > d):
          yield (n, k)
    # consider d-digit numbers, find sets that correspond to exactly 4 numbers
    def set4(k):
      # accumulate numbers by the set of digits
      r = defaultdict(list)
      for n in irange(10 ** (k - 1), (10 ** k) - 1):
        s = join(sorted(str(n)))
      # return sets of 4
      for vs in r.values():
        if len(vs) == 4:
          yield vs
    # find the number of houses <n> and the length of my number <k>
    for (n, k) in generate():
      printf("[{n} houses in mews, my house number has {k} digits]")
      # find sets that correspond to exactly 4 numbers
      # and determine which have exactly two numbers in the mews
      # then look for smallest largest number
      m = Accumulator(fn=min)
      for ns in set4(k):
        if sum(1 for x in ns if x > n) == 2:
          m.accumulate_data(ns[-1], ns)
          printf("[4 number set: {ns}]")
      # opposite house numbers in the mews sum to n + 1
      for x in m.data:
        if not(x > n):
          y = n + 1 - x
          if x != y:
            printf("my house number = {x}, opposite house = {y}")
      # and we're done

    Solution: The number of the house opposite yours is 157.

    The puzzle setters house number is 103. There are 259 houses (numbered 1 to 259) in the mews, and the numbers on opposite pairs of houses sum to 260 (e.g. 1 + 259, 2 + 258, 3 + 257 etc).

    If the mews were extended with houses 260 to 310, then there would be 4 houses whose numbers are composed of the digits 0, 1 and 3. Namely: 103 and 130 (in the original part of the mews) and 301 and 310 (in the new part of the mews). So, the setters house number is either 103 or 130. 103 is opposite 157, but 130 would be at the end of the mews and so have no house opposite, which gives us the unique solution.

    It would be possible for 129 to be opposite 131, so that 130 was the only house at the end of the mews, or for all houses between 104 and 156 to be at the end of the mews, or any subset of these centred around 130. There isn’t enough information for us to determine the exact layout.

    If the mews were extended up to 320 there would then be a second set of 4 houses whose numbers are composed of the same digits, namely 0, 2 and 3. (The houses being 203 and 230 in the original mews and 302 and 320 in the newly extended part). So the mews can be extended up to between 310 and 319 for there to only be a single set of 4 houses that share the same digits.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: