Skip to content

Instantly share code, notes, and snippets.

@brouwerfp
Last active August 31, 2020 14:29
Show Gist options
  • Select an option

  • Save brouwerfp/669a5ac89fdb3f1bb48ee27cc8f11159 to your computer and use it in GitHub Desktop.

Select an option

Save brouwerfp/669a5ac89fdb3f1bb48ee27cc8f11159 to your computer and use it in GitHub Desktop.
// https://twitter.com/tatsuosakamoto/status/1299922512474640387
(()=>{
let strategy = [[]];
let answer = [];
// register approaches to strategy
for(let i=0;i<5;i++){
let next = [];
for(let j=0;j<strategy.length;j++){
let current = strategy[j];
switch(current[i-1]){
case undefined:
case -7:
case 3:
next.push(current.concat(3));
if(i !== 4){
next.push(current.concat(4));
}
break;
case 4:
case -6:
next.push(current.concat(-7));
if(i !== 4){
next.push(current.concat(-6));
}
break;
default:
break;
}
}
strategy = next;
}
//answer: strategy, branch, {subtractee, subtracter}, digit -> [0, 10)
for(let h=0;h<strategy.length;h++){
let st_current = strategy[h];
answer.push([{subtractee: [], subtracter: []}]);
for(let i=0;i<5;i++){
let next = [];
switch(st_current[i]){
case -7:
for(let j=0;j<answer[h].length;j++){
let current = answer[h][j];
next.push({subtractee: current.subtractee.concat(2)
, subtracter: current.subtracter.concat(9)});
next.push({subtractee: current.subtractee.concat(1)
, subtracter: current.subtracter.concat(8)});
next.push({subtractee: current.subtractee.concat(0)
, subtracter: current.subtracter.concat(7)});
}
break;
case -6:
for(let j=0;j<answer[h].length;j++){
let current = answer[h][j];
next.push({subtractee: current.subtractee.concat(3)
, subtracter: current.subtracter.concat(9)});
next.push({subtractee: current.subtractee.concat(2)
, subtracter: current.subtracter.concat(8)});
next.push({subtractee: current.subtractee.concat(1)
, subtracter: current.subtracter.concat(7)});
next.push({subtractee: current.subtractee.concat(0)
, subtracter: current.subtracter.concat(6)});
}
break;
case 3:
for(let j=0;j<answer[h].length;j++){
let current = answer[h][j];
next.push({subtractee: current.subtractee.concat(9)
, subtracter: current.subtracter.concat(6)});
next.push({subtractee: current.subtractee.concat(8)
, subtracter: current.subtracter.concat(5)});
next.push({subtractee: current.subtractee.concat(7)
, subtracter: current.subtracter.concat(4)});
next.push({subtractee: current.subtractee.concat(6)
, subtracter: current.subtracter.concat(3)});
next.push({subtractee: current.subtractee.concat(5)
, subtracter: current.subtracter.concat(2)});
next.push({subtractee: current.subtractee.concat(4)
, subtracter: current.subtracter.concat(1)});
next.push({subtractee: current.subtractee.concat(3)
, subtracter: current.subtracter.concat(0)});
}
break;
case 4:
for(let j=0;j<answer[h].length;j++){
let current = answer[h][j];
next.push({subtractee: current.subtractee.concat(9)
, subtracter: current.subtracter.concat(5)});
next.push({subtractee: current.subtractee.concat(8)
, subtracter: current.subtracter.concat(4)});
next.push({subtractee: current.subtractee.concat(7)
, subtracter: current.subtracter.concat(3)});
next.push({subtractee: current.subtractee.concat(6)
, subtracter: current.subtracter.concat(2)});
next.push({subtractee: current.subtractee.concat(5)
, subtracter: current.subtracter.concat(1)});
next.push({subtractee: current.subtractee.concat(4)
, subtracter: current.subtracter.concat(0)});
}
break;
}
for(let j=0;j<next.length;j++){
let current = next[j];
let used = [];
let flag = true;
for(let k=0;flag && k<current.subtractee.length;k++){
if(used.some(e=>(e === current.subtractee[k]))){
flag = false;
}
used.push(current.subtractee[k]);
}
for(let k=0;flag && k<current.subtracter.length;k++){
if(used.some(e=>(e === current.subtracter[k]))){
flag = false;
}
used.push(current.subtracter[k]);
}
if(!flag)
next[j] = undefined;
}
answer[h] = next.filter(e => e);
}
//beautification
answer[h] = answer[h].map(e => e.subtractee.join("") + " - " + e.subtracter.join("") + " = 33333").join("\n");
strategy[h] = strategy[h].map(e => "make " + e).join(" & ");
}
return strategy.map((e, i)=>({strategy: e, answer: answer[i]}));
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment