Created
May 25, 2022 17:28
-
-
Save umutbasal/9986183a103dc86df29321a07ef67e98 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const problem = { | |
operators: { | |
has: (possibility, numbers, has) => possibility.reduce((acc, cur) => numbers.includes(cur) ? acc + 1 : acc, 0) === has, | |
rightPlace: (possibility, numbers) => possibility.some(x => possibility.indexOf(x) === numbers.indexOf(x)), | |
}, | |
possibilities: Array.from(Array(1000).keys()).map(x => x.toString().padStart(3, '0').split('').map(x => parseInt(x))), | |
sets: [ | |
{ numbers: [6, 9, 0], has: 1, rightPlace: true }, // One number is correct and right place | |
{ numbers: [7, 4, 1], has: 1, rightPlace: false }, // One number is correct but wrong place | |
{ numbers: [5, 0, 4], has: 2, rightPlace: false }, // Two numbers are correct but wrong place | |
{ numbers: [3, 8, 7], has: 0, rightPlace: false }, // Nothing is correct | |
{ numbers: [2, 1, 9], has: 1, rightPlace: false }, // One number is correct but wrong place | |
] | |
}; | |
let possibilities = problem.possibilities; | |
problem.sets.forEach(set => { | |
possibilities = possibilities | |
.filter(possibility => problem.operators.has(possibility, set.numbers, set.has)) | |
.filter(possibility => problem.operators.rightPlace(possibility, set.numbers) === set.rightPlace); | |
}) | |
console.log(possibilities); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
and some joke