Last active
January 28, 2020 01:14
-
-
Save diegodorado/0264843d77ae015131e94b83298127d1 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
let digits = "0123456789".split('') | |
function *permute(a, n = a.length) { | |
if (n <= 1) yield a.slice(); | |
else for (let i = 0; i < n; i++) { | |
yield *permute(a, n - 1); | |
const j = n % 2 ? 0 : i; | |
[a[n-1], a[j]] = [a[j], a[n-1]]; | |
} | |
} | |
var start = new Date(); | |
console.log('brute force search started...'); | |
Array.from(permute(digits)).forEach( arr => { | |
let e,a,r,t,h,v,n,u,s | |
e=arr[0] | |
a=arr[1] | |
r=arr[2] | |
t=arr[3] | |
h=arr[4] | |
v=arr[5] | |
n=arr[6] | |
u=arr[7] | |
s=arr[8] | |
let earth = parseInt(e+a+r+t+h) | |
let venus = parseInt(v+e+n+u+s) | |
let uranus = parseInt(u+r+a+n+u+s) | |
let saturn = parseInt(s+a+t+u+r+n) | |
if(earth+venus+uranus === saturn){ | |
let solution = ` | |
EARTH | |
${e+a+r+t+h} | |
VENUS | |
${v+e+n+u+s} | |
URANUS | |
${u+r+a+n+u+s} | |
------------ | |
SATURN | |
${s+a+t+u+r+n} | |
` | |
console.log(solution) | |
} | |
}) | |
var end = new Date(); | |
var time = end.getTime() - start.getTime(); | |
console.log('finished in', time, 'ms'); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment