Created
November 23, 2015 18:29
-
-
Save srounce/24e29d0a9875bd6c89ac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
let AsciiTable = require('ascii-table') | |
let solve = (hints) => { | |
function permutate(dictionary) { | |
const results = [] | |
const permute = (array, position) => { | |
if (position === array.length - 1) { | |
results.push(array) | |
} else { | |
for (let i = position; i < array.length; i++) { | |
const swap = array[position] | |
array.splice(position, 1, array[i]) | |
array.splice(i, 1, swap) | |
permute(array.slice(), (position + 1)) | |
} | |
} | |
} | |
permute(dictionary, 0) | |
return results | |
} | |
const colors = permutate(mapType(Colors)) | |
const drinks = permutate(mapType(Drinks)) | |
const smokes = permutate(mapType(Smokes)) | |
const pets = permutate(mapType(Pets)) | |
const nationalities = permutate(mapType(Nationalities)) | |
nationalities.forEach(n => { | |
n[0] === Nationalities.NORWEGIAN && | |
smokes.forEach(s => { | |
s[n.indexOf(Nationalities.GERMAN)] === Smokes.PRINCE && | |
pets.forEach(p => { | |
p[n.indexOf(Nationalities.SWEDISH)] === Pets.DOG && | |
p[s.indexOf(Smokes.PALL_MALL)] === Pets.BIRD && | |
Math.abs(s.indexOf(Smokes.BLEND) - p.indexOf(Pets.CAT)) === 1 && | |
Math.abs(s.indexOf(Smokes.DUNHILL) - p.indexOf(Pets.HORSE)) === 1 && | |
drinks.forEach(d => { | |
d[n.indexOf(Nationalities.DANISH)] === Drinks.TEA && | |
d[2] === Drinks.MILK && | |
d[s.indexOf(Smokes.BLUE_MASTER)] === Drinks.BEER && | |
Math.abs(s.indexOf(Smokes.BLEND) - d.indexOf(Drinks.WATER)) === 1 && | |
colors.forEach(c => { | |
c[1] === Colors.BLUE && | |
c[n.indexOf(Nationalities.NORWEGIAN) + 1] === Colors.BLUE && | |
c[n.indexOf(Nationalities.BRITISH)] === Colors.RED && | |
c[s.indexOf(Smokes.DUNHILL)] === Colors.YELLOW && | |
c[d.indexOf(Drinks.COFFEE)] === Colors.GREEN && | |
c.indexOf(Colors.GREEN) - c.indexOf(Colors.WHITE) === -1 && | |
console.log(AsciiTable.factory({ | |
//rows: houses.map((h) => Object.keys(h).map((c) => h[c])) | |
rows: ((a) => { | |
return a[0].map((_, c) => { | |
return a.map((r) => r[c]) | |
}) | |
})([n, s, p, d, c]) | |
}).toString()) | |
}) | |
}) | |
}) | |
}) | |
}) | |
} | |
let mapType = (c) => Object.keys(c).map((v) => c[v]) | |
const Nationalities = { | |
BRITISH: 'British', | |
NORWEGIAN: 'Norwegian', | |
GERMAN: 'German', | |
SWEDISH: 'Swedish', | |
DANISH: 'Danish' | |
} | |
const Colors = { | |
RED: 'Red', | |
BLUE: 'Blue', | |
GREEN: 'Green', | |
WHITE: 'White', | |
YELLOW: 'Yellow' | |
} | |
const Smokes = { | |
DUNHILL: 'Dunhill', | |
BLUE_MASTER: 'BlueMaster', | |
BLEND: 'Blend', | |
PALL_MALL: 'Pall Mall', | |
PRINCE: 'Prince' | |
} | |
const Drinks = { | |
WATER: 'Water', | |
BEER: 'Beer', | |
TEA: 'Tea', | |
COFFEE: 'Coffee', | |
MILK: 'Milk' | |
} | |
const Pets = { | |
DOG: 'Dog', | |
CAT: 'Cat', | |
HORSE: 'Horse', | |
FISH: 'Fish', | |
BIRD: 'Bird' | |
} | |
var hints = [ | |
[{ nationality: Nationalities.BRITISH }, { color: Colors.RED }], | |
[{ nationality: Nationalities.SWEDISH }, { pet: Pets.DOG }], | |
[{ nationality: Nationalities.DANISH }, { drink: Drinks.TEA }], | |
[{ color: Colors.GREEN }, { leftOf: Colors.WHITE }], | |
[{ color: Colors.GREEN }, { drink: Drinks.COFFEE }], | |
[{ smokes: Smokes.PALL_MALL }, { pet: Pets.BIRD }], | |
[{ color: Colors.YELLOW }, { smokes: Smokes.DUNHILL }], | |
[{ index: 2 }, { drinks: Drinks.MILK }], | |
[{ nationality: Nationalities.NORWEGIAN }, { index: 0 }], | |
[{ smokes: Smokes.BLEND }, { nextTo: Pets.CAT }], | |
[{ pets: Pets.HORSE }, { nextTo: Smokes.DUNHILL }], | |
[{ smokes: Smokes.BLUE_MASTER }, { drinks: Drinks.BEER }], | |
[{ nationality: Nationalities.GERMAN }, { smokes: Smokes.PRINCE }], | |
[{ nationality: Nationalities.NORWEGIAN }, { nextTo: Colors.BLUE }], | |
[{ smokes: Smokes.BLEND }, { nextTo: Drinks.WATER }] | |
] | |
solve(hints); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.--------------------------------------------------. | |
| | | | | | | |
|-----------|------------|-------|--------|--------| | |
| Norwegian | Dunhill | Cat | Water | Yellow | | |
| Danish | Blend | Horse | Tea | Blue | | |
| British | Pall Mall | Bird | Milk | Red | | |
| German | Prince | Fish | Coffee | Green | | |
| Swedish | BlueMaster | Dog | Beer | White | | |
'--------------------------------------------------' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "einstein", | |
"version": "1.0.0", | |
"description": "", | |
"main": "einstein.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"ascii-table": "0.0.8" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment