-
-
Save Mellen/1dd05482de9a8f8c72803cfca90f3b15 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
(function() | |
{ | |
let input = document.querySelector('pre').innerHTML.trim().split('\n'); | |
let alergens = new Set(input.map(line => | |
{ | |
return line.split('(contains ')[1].replace(')', '').split(', '); | |
}).flat()); | |
let allwords = input.reduce((words, line) => | |
{ | |
let linewords = line.split('(')[0].trim().split(' '); | |
return words.concat(linewords); | |
},[]); | |
let guesses = [...alergens].reduce((map, alergen) => | |
{ | |
let guess = input.reduce((coll, line) => | |
{ | |
if(line.includes(alergen)) | |
{ | |
let nonsense = line.split('(')[0].trim().split(' ') | |
if(coll.length == 0) | |
{ | |
coll = nonsense; | |
} | |
else | |
{ | |
coll = coll.filter(ingred => nonsense.includes(ingred)); | |
} | |
} | |
return coll; | |
},[]); | |
map.set(alergen, guess); | |
return map; | |
}, new Map()); | |
let flatguesses = [...guesses.values()].flat(); | |
let goodIngred = allwords.filter(word => !flatguesses.includes(word)); | |
return goodIngred; | |
})() | |
(function() | |
{ | |
let input = document.querySelector('pre').innerHTML.trim().split('\n'); | |
let alergens = new Set(input.map(line => | |
{ | |
return line.split('(contains ')[1].replace(')', '').split(', '); | |
}).flat()); | |
let guesses = [...alergens].reduce((map, alergen) => | |
{ | |
let guess = input.reduce((coll, line) => | |
{ | |
if(line.includes(alergen)) | |
{ | |
let nonsense = line.split('(')[0].trim().split(' ') | |
if(coll.length == 0) | |
{ | |
coll = nonsense; | |
} | |
else | |
{ | |
coll = coll.filter(ingred => nonsense.includes(ingred)); | |
} | |
} | |
return coll; | |
},[]); | |
map.set(alergen, guess); | |
return map; | |
}, new Map()); | |
for(let i = 0; i < 3; i++) | |
{ | |
for(let entry of guesses.entries()) | |
{ | |
if(entry[1].length == 1) | |
{ | |
let code = entry[1][0]; | |
for(let oentry of guesses.entries()) | |
{ | |
if(oentry[0] == entry[0]) | |
{ | |
continue; | |
} | |
let oindex = oentry[1].indexOf(code); | |
if(oindex > -1) | |
{ | |
oentry[1].splice(oindex,1); | |
} | |
} | |
} | |
} | |
} | |
return [...guesses.entries()].sort((a, b) => a>b) | |
.map(guess => guess[1][0]) | |
.join(','); | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment