Created
January 3, 2017 21:06
-
-
Save anonymous/b6428b9e291519001c1ac0439a9fca44 to your computer and use it in GitHub Desktop.
arithmetic puzzle
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 state = [[6,1,"6"],[6,2,"6"],[5,4,"5"],[2,8,"2"]]; | |
function go(x, y, f, g) { | |
if ((x[1] & y[1]) == 0) { | |
state.push([f(x[0], y[0]), x[1] | y[1], g(x[2], y[2])]); | |
} | |
} | |
for (var pass = 0; pass < 3; pass++) { | |
const L = state.length; | |
for (var i = 0; i < L; i++) { | |
for (var j = 0; j < L; j++) { | |
go(state[i], state[j], (x, y) => x - y, (x, y) => `(${x}-${y})`); | |
go(state[i], state[j], (x, y) => x / y, (x, y) => `(${x}/${y})`); | |
} | |
} | |
for (var i = 0; i < L; i++) { | |
for (var j = i+1; j < L; j++) { | |
go(state[i], state[j], (x, y) => x * y, (x, y) => `(${x}*${y})`); | |
go(state[i], state[j], (x, y) => x + y, (x, y) => `(${x}+${y})`); | |
} | |
} | |
} | |
state.forEach(x => { if (x[0] == 17 && x[1] == 15) console.log(x[2])}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment