Skip to content

Instantly share code, notes, and snippets.

Created January 3, 2017 21:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/b6428b9e291519001c1ac0439a9fca44 to your computer and use it in GitHub Desktop.
Save anonymous/b6428b9e291519001c1ac0439a9fca44 to your computer and use it in GitHub Desktop.
arithmetic puzzle
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