Created
September 18, 2020 04:46
-
-
Save axel669/35c62e3c3eacd05030625895f4661e0c to your computer and use it in GitHub Desktop.
Countdown Numbers Code Golf
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 v=null,ops = [["+",(a,b)=>a+b],["-",(a,b)=>(a<=b)?v:a-b],["*",(a,b)=>(a==1||b==1)?v:a*b],["/",(a,b)=>(a==1||b==1||a<=b||(a%b)!=0)?v:a/b]],p=a=>{let l=[];a.forEach((n,i)=>l.push(...a.slice(i + 1).map(m=>[n,m])));return l},x=[0,1,2],c=[p([0,1]),p(x),p([...x,3]),p([...x,3,4]),p([...x,3,4,5])],l="length",w={d:Infinity,p:Array.from({[l]:6})},u=(t,y,h=[],b={...w})=>{if (y[l]==1)return;let z = c[y[l]-2];y.sort((a,b)=>b-a);for(let[r,q]of z){for(let[o,k]of ops){let f=y[r],s=y[q],j=k(f, s);if(j!=v){let g=[j,...y.filter((_,i)=>i!=r&&i!=q)],p=[...h,[f,s,o]],d=Math.min(...g.map(n=>Math.abs(t-n))),i=d<b.d||(d==b.d&&p[l]<b.p[l]);if(i){b.d=d;b.p=p}if(!g.includes(t))u(t,g,p,b)}}}return{dist:b.d,path:b.p}},check=u; | |
console.log(check(813, [25, 50, 75, 100, 1, 10])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment