Scenes from my programming past: Puzzles III

Continuing through this set of five puzzles from 40 years ago (one, two) that I had to solve using FORTRAN on punched cards on King’s College’s timeshare.

The third puzzle is perhaps the most tedious of all so far. Let’s see what it says:

One Christmas treat we, alas, never miss is Aunt Millicent’s smell game. She puts five of what she terms ‘substances’ in muslin bags and bids us take a good sniff in turn. Her sense of humour is very much her own as you will see from this record of guesses from last Xmas.

Person Bag A Bag B Bag C Bag D Bag E
Julian sardine garlic camphor snuff bad egg
Katrina anchovy garlic mint curry sal volatile
Len kipper cloves verbena curry bad egg
Mike whale garlic camphor snuff vinegar
Nancy mouse nutmeg verbena snuff dry rot

 

This year, we each got exactly 2 guesses right. What was in the bags?

In case you didn’t know, sal volatile is another name for smelling salts, but I’d have to say the whole thing reads like something Agatha Christie would write on an off day. And although ‘whale’ is bad enough, ‘mouse’? Ewwww…

In essence, to solve the problem becomes a grand loop of loops. There’s no simple analysis that I could see to optimize it in any way. There are 5 possibilities for bag A, 3 for B and C, 2 for D ,and 4 for E. A total of 360 combinations. The solution is, therefore, to cycle through all 360 of them and count the number of correct guesses for each person. If there’s a solution where each person guessed 2 right, bingo: print it and stop (although I couldn’t be bothered to stop the code – it also helped to see that there was really only one answer).

/*jslint white this */
/*global console*/
(function () {
  "use strict";

  var sardine = 0;
  var anchovy = 1;
  var kipper = 2;
  var whale = 3;
  var mouse = 4;
  var garlic = 5;
  var cloves = 6;
  var nutmeg = 7;
  var camphor = 8;
  var mint = 9;
  var verbena = 10;
  var snuff = 11;
  var curry = 12;
  var badEgg = 13;
  var salVolatile = 14;
  var vinegar = 15;
  var dryRot = 16;

  var bagAPossibilities = [sardine, anchovy, kipper, whale, mouse];
  var bagBPossibilities = [garlic, cloves, nutmeg];
  var bagCPossibilities = [camphor, mint, verbena];
  var bagDPossibilities = [snuff, curry];
  var bagEPossibilities = [badEgg, salVolatile, vinegar, dryRot];

  var guesses = [
    [sardine, garlic, camphor, snuff, badEgg],
    [anchovy, garlic, mint, curry, salVolatile],
    [kipper, cloves, verbena, curry, badEgg],
    [whale, garlic, camphor, snuff, vinegar],
    [mouse, nutmeg, verbena, snuff, dryRot]
  ];

  var printAnswer = function(p){
    var contents = [
      "sardine",
      "anchovy",
      "kipper",
      "whale",
      "mouse",
      "garlic",
      "cloves",
      "nutmeg",
      "camphor",
      "mint",
      "verbena",
      "snuff",
      "curry",
      "bad egg",
      "sal volatile",
      "vinegar",
      "dry rot"
    ];

    console.log("Bag A contains", contents[p[0]]);
    console.log("Bag B contains", contents[p[1]]);
    console.log("Bag C contains", contents[p[2]]);
    console.log("Bag D contains", contents[p[3]]);
    console.log("Bag E contains", contents[p[4]]);
  };

  var checkCounts = function(p) {
    return guesses.find(function(g) {
      var count = 0;
      p.forEach(function(possibility, i) {
        if (possibility === g[i]) {
          count += 1;
        }
      });
      return count !== 2;
    }) === undefined;
  };
  
  var cycleThroughPossibilities = function() {
    bagAPossibilities.forEach(function(pa) {
      bagBPossibilities.forEach(function(pb) {
        bagCPossibilities.forEach(function(pc) {
          bagDPossibilities.forEach(function(pd) {
            bagEPossibilities.forEach(function(pe) {
              var possibility = [pa, pb, pc, pd, pe];
              if (checkCounts(possibility)) {
                printAnswer(possibility);
              };
            });
          });
        });
      });
    });
  };

  cycleThroughPossibilities();
}());

Not much to say here apart from the fact that typing it all in was the most tedious part of it. It almost cries out for a gazillion bare for loops but I RESISTED!

The answer is:

Bag A contains kipper
Bag B contains garlic
Bag C contains verbena
Bag D contains snuff
Bag E contains sal volatile

(Aside: in the original hand-written pages, I’d crossed out Len’s guess for bag D of ‘snuff’ and replaced it with ‘curry’. I don’t know why, because if you revert to the original in the code, it still just produces a single valid answer.)

All in all, a pretty meh puzzle, but one that was possibly easier to write in FORTRAN without too much effort.

Garlic

Loading similar posts...   Loading links to posts on similar topics...

No Responses

Feel free to add a comment...

Leave a response

Note: some MarkDown is allowed, but HTML is not. Expand to show what's available.

  •  Emphasize with italics: surround word with underscores _emphasis_
  •  Emphasize strongly: surround word with double-asterisks **strong**
  •  Link: surround text with square brackets, url with parentheses [text](url)
  •  Inline code: surround text with backticks `IEnumerable`
  •  Unordered list: start each line with an asterisk, space * an item
  •  Ordered list: start each line with a digit, period, space 1. an item
  •  Insert code block: start each line with four spaces
  •  Insert blockquote: start each line with right-angle-bracket, space > Now is the time...
Preview of response