Skip to content

Instantly share code, notes, and snippets.

@Spuffynism
Last active January 22, 2018 22:06
Show Gist options
  • Save Spuffynism/ee9b25443882dfd453cc8a1a244de9d1 to your computer and use it in GitHub Desktop.
Save Spuffynism/ee9b25443882dfd453cc8a1a244de9d1 to your computer and use it in GitHub Desktop.
do you know the way generator
// this is a do-you-know-the-way sentence generator
var words = [
["do"],
["you", "u"],
["know", "no", "kno"],
["da", "de", "the"],
["way", "wey", "wei", "wae"]],
combinations = [],
combinationNb = words.reduce((acc, v) => acc * v.length, 1);
for (var i = 0; i < combinationNb; i++)
combinations.push([]);
for (var i = 0; i < words.length; i++) {
for (var j = 0, k = 0; j < combinationNb; j++, k = j % words[i].length) {
combinations[j].push(words[i][k]);
}
}
var sentences = combinations
.map(c =>
c.reduce((acc, v, i) => acc + (i === 0 ? "" : " ") + v, ""));
console.log(sentences);
// codegolfed
for(i=0,w=[["do"],["you","u"],["know","no","kno"],["da","de","the"],["way","wey","wei","wae"]],t=[];i<w.length;i++)for(var j=0,k=0;j<w.reduce((a,v)=>a*v.length,1);j++,k=j%w[i].length)t[j]=(i?t[j]+" ":"")+w[i][k];console.log(t);
// 216
for(i=0,w=[['do'],['you','u'],['know','no','kno'],['da','de','the'],['way','wey','wei','wae']],t=[],l='length';i++<w[l];)for(j=k=0;j++<w.reduce((a,v)=>a*v[l],1);k=j%w[i][l])t[j]=(i?t[j]+' ':'')+w[i][k];console.log(t)
// 202
t=[],w=[['do'],['you','u'],['know','no','kno'],['da','de','the'],['way','wey','wei','wae']],w.map((e,i)=>{for(j=0;j++<w.reduce((a,v)=>a*v[l],1);l='length')t[j]=(i?t[j]+' ':'')+e[j%e[l]]});console.log(t)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment