Skip to content

Instantly share code, notes, and snippets.

@carlos8f
Last active January 10, 2021 08:25
Show Gist options
  • Save carlos8f/284814e92e155d43f2cd to your computer and use it in GitHub Desktop.
Save carlos8f/284814e92e155d43f2cd to your computer and use it in GitHub Desktop.
einstein riddle solver
#!/usr/bin/env node
var assert = require('assert');
var houses = [
'polka dot',
'tartan',
'gingham',
'paisley',
'striped'
];
var people = [
'nicola',
'nigel',
'david',
'ed',
'nick'
];
var pets = [
'squirrel',
'fish',
'guinnea pig',
'pitbull',
'badger'
];
var coffees = [
'flat whites',
'double espresso',
'chai latte',
'decaf',
'mochaccino'
];
var transports = [
'car',
'plane',
'foot',
'train',
'bike'
];
function randomOrder (arr) {
var arrCopy = arr.slice();
arrCopy.sort(function (a, b) {
return Math.random() >= 0.5 ? 1 : -1;
});
return arrCopy;
}
function newCandidate () {
var data = {
houses: houses,
people: people.slice(),
pets: pets.slice(),
coffees: coffees.slice(),
transports: transports.slice()
};
function randomChoice (type) {
var len = data[type].length;
var idx = Math.ceil(Math.random() * len) - 1;
if (idx === -1) idx = 0;
if (typeof data[type][idx] === 'undefined') throw new Error('undefined idx: ' + type + ', ' + idx);
var choice = data[type].splice(idx, 1)[0];
return choice;
}
return randomOrder(houses).map(function (house, idx) {
return {
house: house,
houseIdx: Number(idx),
person: randomChoice('people'),
pet: randomChoice('pets'),
coffee: randomChoice('coffees'),
transport: randomChoice('transports')
};
});
}
var solution;
do {
var c = newCandidate();
function find (val) {
var house;
for (var idx = 0; idx < c.length; idx++) {
Object.keys(c[idx]).forEach(function (k) {
if (c[idx][k] == val) house = c[idx];
});
}
if (!house) throw new Error('could not find val: ' + val);
return house;
}
try {
// 1
var nicola = find('nicola');
assert.equal(nicola.house, 'tartan');
// 2
var ed = find('ed');
assert.equal(ed.pet, 'guinnea pig');
// 3
var david = find('david');
assert.equal(david.coffee, 'mochaccino');
// 4
var paisley = find('paisley');
var gingham = find('gingham');
assert.equal(gingham.houseIdx - 1, paisley.houseIdx);
// 5
assert.equal(paisley.coffee, 'flat whites');
// 6
var car = find('car');
assert.equal(car.pet, 'squirrel');
// 7
var striped = find('striped');
assert.equal(striped.transport, 'bike');
// 8
var centre = c[2];
assert.equal(centre.coffee, 'double espresso');
// 9
var nick = find('nick');
assert.equal(nick.houseIdx, 0);
// 10
var train = find('train');
var pitbull = find('pitbull');
assert.equal(Math.abs(train.houseIdx - pitbull.houseIdx), 1);
// 11
var badger = find('badger');
var bike = find('bike');
assert.equal(Math.abs(badger.houseIdx - bike.houseIdx), 1);
// solved in 2m
// 12
var chai = find('chai latte');
// assert.equal(chai.transport, 'plane');
// 13
var nigel = find('nigel');
assert.equal(nigel.transport, 'foot');
solution = c;
break;
// 14
var polka = find('polka dot');
assert.equal(Math.abs(nick.houseIdx - polka.houseIdx), 1);
// 15
var decaf = find('decaf');
assert.equal(Math.abs(train.houseIdx - decaf.houseIdx), 1);
solution = c;
}
catch (e) {}
}
while (!solution);
console.log(JSON.stringify(solution, null, 2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment