Skip to content

Instantly share code, notes, and snippets.

@nainemom
Last active July 4, 2019 22:31
Show Gist options
  • Save nainemom/d039aeb32d99032fe1cf32e58ba5386e to your computer and use it in GitHub Desktop.
Save nainemom/d039aeb32d99032fe1cf32e58ba5386e to your computer and use it in GitHub Desktop.
Einstein's Five-House Riddle Solver with JavaScript
// Einstein's Five-House Riddle Solver
// Original question: https://udel.edu/~os/riddle.html
// by Nainemom <[email protected]>
const print = (colors, nations, drinks, pets, cigars, time) => {
console.log('')
console.log('-'.repeat(103))
console.log(['#1', '#2', '#3', '#4', '#5'].join("\t\t\t"))
console.log('-'.repeat(103))
console.log(colors.join("\t\t\t"))
console.log(nations.join("\t\t\t"))
console.log(drinks.join("\t\t\t"))
console.log(pets.join("\t\t\t"))
console.log(cigars.join("\t\t\t"))
console.log('-'.repeat(103))
console.log(`This result calced in ${time}ms`)
console.log('')
}
// from https://stackoverflow.com/questions/9960908/permutations-in-javascript
const permutator = inputArr => {
let result = []
const permute = (arr, m = []) => {
if (arr.length === 0) {
result.push(m)
} else {
for (let i = 0; i < arr.length; i++) {
let curr = arr.slice()
let next = curr.splice(i, 1)
permute(curr.slice(), m.concat(next))
}
}
}
permute(inputArr)
return result
}
const runTests = (colors, nations, drinks, pets, cigars) =>
drinks.indexOf('milk') === 2 && // hint 8
(nations.indexOf('norweg') === 0 || nations.indexOf('norweg') === 4) && // hint 9
nations.indexOf('brit') === colors.indexOf('red') && // hint 1
nations.indexOf('swede') === pets.indexOf('dog') && // hint 2
nations.indexOf('dane') === drinks.indexOf('tea') && // hint 3
colors.indexOf('green') === drinks.indexOf('coffee') && // hint 5
cigars.indexOf('pllmll') === pets.indexOf('bird') && // hint 6
cigars.indexOf('blmstr') === drinks.indexOf('beer') && // hint 12
nations.indexOf('german') === cigars.indexOf('prince') && // hint 13
colors.indexOf('green') + 1 === colors.indexOf('white') && // hint 4
colors.indexOf('yellow') === cigars.indexOf('dunhill') && // hint 7
(cigars.indexOf('blend') + 1 === pets.indexOf('cat') || cigars.indexOf('blend') - 1 === pets.indexOf('cat')) && // hint 10
(pets.indexOf('horse') + 1 === cigars.indexOf('dunhill') || pets.indexOf('horse') - 1 === cigars.indexOf('dunhill')) && // hint 11
(nations.indexOf('norweg') + 1 === colors.indexOf('blue') || nations.indexOf('norweg') - 1 === colors.indexOf('blue')) && // hint 14
(cigars.indexOf('blend') + 1 === drinks.indexOf('water') || cigars.indexOf('blend') - 1 === drinks.indexOf('water')) // hint 15
const colors = permutator(['red', 'green', 'white', 'yellow', 'blue'])
const nations = permutator(['swede', 'brit', 'dane', 'norweg', 'german'])
const drinks = permutator(['tea', 'coffee', 'milk', 'beer', 'water'])
const pets = permutator(['bird', 'cat', 'horse', 'dog', 'fish'])
const cigars = permutator(['pllmll', 'dunhill', 'blend', 'blmstr', 'prince'])
const calcResult = () => {
console.log('Process started. Please wait...')
console.log('[Loading Animation]')
let start = Date.now()
const result = []
for (let cColors of colors) {
for (let cNations of nations) {
for (let cDrinks of drinks) {
for (let cPets of pets) {
for (let cCigars of cigars) {
if (runTests(cColors, cNations, cDrinks, cPets, cCigars)) {
print(cColors, cNations, cDrinks, cPets, cCigars, Date.now() - start)
start = Date.now()
}
}
}
}
}
}
return result
}
calcResult()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment