Skip to content

Instantly share code, notes, and snippets.

@fabienhinault
Last active March 20, 2025 12:01
Show Gist options
  • Save fabienhinault/ee211677f110182ed1f1bed4449ca7f1 to your computer and use it in GitHub Desktop.
Save fabienhinault/ee211677f110182ed1f1bed4449ca7f1 to your computer and use it in GitHub Desktop.
M12
move_m = ([a,b,c,d,e,f,g,h,i,j,k,l]) => [a,l,b,k,c,j,d,i,e,h,f,g];
move_i = ([a,b,c,d,e,f,g,h,i,j,k,l]) => [l,k,j,i,h,g,f,e,d,c,b,a];
mm = (count, a) => {if (count === 0) return a; else return move_m(mm(count -1, a));};
jouer = (ms, d) => to12(ms.reduce((acc, v) => mm(v, move_i(acc)), move_i(d)));
d = [1,2,3,4,5,6,7,8,9,10,11,12];
sol = (ms) => ms.map(m => (11 - m) % 11).reverse();
sols = new Map();
next = (jeu) => {
let lastIndex = jeu.findLastIndex((i) => i < 10);
if (lastIndex === -1) {
let res = new Array(jeu.length + 1);
res.fill(1);
return res;
} else {
jeu[lastIndex]++;
for (i = lastIndex + 1; i < jeu.length; i++) {
jeu[i] = 1;
}
return jeu;
}
}
to12 = (d) => {
let res = move_i(d);
if (res[0] !== 1) {
while(res[res.length - 1] !== 1) {
res = move_m(res);
}
res = move_i(res);
}
while (res[1] !== 2) {
res = move_m(res);
}
return res;
}
// https://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript
// Warn if overriding existing method
if(Array.prototype.equals)
console.warn("Overriding existing Array.prototype.equals. Possible causes: New API defines the method, there's a framework conflict or you've got double inclusions in your code.");
// attach the .equals method to Array's prototype to call it on any array
Array.prototype.equals = function (array) {
// if the other array is a falsy value, return
if (!array)
return false;
// if the argument is the same array, we can be sure the contents are same as well
if(array === this)
return true;
// compare lengths - can save a lot of time
if (this.length != array.length)
return false;
for (var i = 0, l=this.length; i < l; i++) {
// Check if we have nested arrays
if (this[i] instanceof Array && array[i] instanceof Array) {
// recurse into the nested arrays
if (!this[i].equals(array[i]))
return false;
}
else if (this[i] != array[i]) {
// Warning - two different object instances will never be equal: {x:20} != {x:20}
return false;
}
}
return true;
}
// Hide method from for-in loops
Object.defineProperty(Array.prototype, "equals", {enumerable: false});
r = [];
a = [1, 0];
while (!r.slice(0,3).equals([1,2,3])){
a = next(a); console.log(a); r = jouer(a, d);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment