-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplank-puzzle-solver.min.js
More file actions
1 lines (1 loc) · 7.8 KB
/
plank-puzzle-solver.min.js
File metadata and controls
1 lines (1 loc) · 7.8 KB
1
!function(t,e,s){const i=[{name:"aaabcd-7",t:"860373112111132125323503132535",i:"25621223440167245223"},{name:"abcccd-7",t:"860072003060642124515312153233",i:"30362254134723522611"},{name:"bcd-10",t:"860474303460620434",i:"3136324516215403473256"},{name:"bbc-16",t:"860074002022522545",i:"40235246225313631473245"},{name:"bbcd-16",t:"8605756064212332520535",i:"31362242351231463037"},{name:"bccd-17",t:"8602742151024242452545",i:"313622530462152373246"},{name:"bbccd-17",t:"86027010302151023234542565",i:"31372253036214235226"},{name:"abb-22",t:"860575204005151535",i:"3124335622541246235501367"},{name:"abcc-21",t:"8604752124121304242454",i:"4123622521631353025512467"},{name:"aabbcd-21",t:"860372101120233132515303232464",i:"3126313523730252263356"},{name:"abbccd-21",t:"860174505301212125414362654353",i:"3125302431362452373126"},{name:"abbcdd-22",t:"860272206011510232535564651535",i:"2262154034722522641356"},{name:"aabb-24",t:"8601733032012123331415",i:"41346302431354236731453146"},{name:"abc-27",t:"860374303142450323",i:"41356133146402354136741245"},{name:"bbccc-31",t:"86047340431113515404241545",i:"32463135226314730253146"},{name:"abbcd-32",t:"86037210203133033324643555",i:"5123462364145720322641235"},{name:"abcd-34",t:"8603744044033333355363",i:"4124522631364035624741236"},{name:"abbbb-36",t:"86027160624143021212324345",i:"1634574013631243356214"},{name:"abbc-38",t:"8600720030121363653454",i:"3036324541257314632353256"}];class n{constructor(t,e){this.x=t,this.y=e}o(t){return s.sqrt((this.x-t.x)**2+(this.y-t.y)**2)}l(t){return this.x==t.x&&this.y==t.y}h(){return`${this.x},${this.y}`}}class o{constructor(t,e){this.p1=t,this.p2=e}get u(){return this.p1.y==this.p2.y}get p(){return this.p1.y==this.p2.y}h(){let{p1:t,p2:e}=this;return t.x<e.x||t.y<e.y?`${t.h()},${e.h()}`:`${e.h()},${t.h()}`}static g(t){let e=t.split(",").map(Number);return new o(new n(e[0],e[1]),new n(e[2],e[3]))}l(t){let{p1:e,p2:s}=this;return t.m(e)&&t.m(s)}m(t){return this.p1.l(t)||this.p2.l(t)}S(t){let{p1:e,p2:s}=this;return t!=this&&(t.m(e)||t.m(s))}get u(){return this.p1.y==this.p2.y}get p(){return this.p1.x==this.p2.x}get k(){let{p1:t,p2:e}=this;return s.abs(t.x-e.x+t.y-e.y)}}class r extends o{constructor(t,e,s=!1){super(t,e),this.v=s,this.C=!1}set p1(t){super.p1=t}set p2(t){super.p2=t}h(){return`${super.h()},${+this.v}`}static g(t){let e=t.split(",").map(Number);return new r(new n(e[0],e[1]),new n(e[2],e[3]),1===e[4])}static $(t){return new r(t.p1,t.p2)}}class c{constructor(t,e,s,i,n,o,r){this.width=t,this.height=e,this.i=s,this.start=i,this.end=n,this.T=o,this.N={},this.R=r,this.A(i)}A(t){for(const e of this.R)if(!e.v&&e.m(t)){e.v=!0;let s=t.l(e.p1)?e.p2:e.p1;this.A(s)}}F(t){t.v&&(this.R.forEach((e=>{e!=t&&e.C&&(e.C=!1)})),t.C=!t.C)}H(t){for(const e of this.R)if(t.k==e.k&&t.l(e))return!0;return!1}Y(t,e){if(t.u===e.u||1==t.k||1==e.k)return!1;let{p1:s,p2:i}=t,{p1:n,p2:o}=e;return t.u?(s.x-n.x)*(i.x-n.x)<0&&(s.y-n.y)*(s.y-o.y)<0:(s.y-n.y)*(i.y-n.y)<0&&(s.x-n.x)*(s.x-o.x)<0}P(){return this.R.some((t=>t.m(this.end)))}W(t){let e=this.R.find((t=>t.C));if(!e)return!1;if(t=t.filter((t=>t.k==e.k&&!this.H(t))),t=t.filter((t=>this.R.some((e=>e.v&&e.S(t))))),!t.length)return!1;for(const t of this.R)t.v=!1;let s=t[0];return e.C=!1,e.p1=s.p1,e.p2=s.p2,l.A(s.p1),!0}X(){let t=this.R.filter((t=>t.v)),e=[],s={},i=this.R.map((t=>t.v)),n=()=>this.R.forEach(((t,e)=>t.v=i[e])),o=()=>this.R.forEach((t=>t.v=!1));for(const i of t){let{p1:r,p2:c}=i,h=this.T.filter((t=>t.k==i.k&&!this.H(t)));if(h=h.filter((e=>{for(const s of t)if(e.S(s))return!0;return!1})),h=h.filter((t=>{for(const e of this.R)if(this.Y(t,e))return!1;return!0})),h.length>0){for(const t of h){i.p1=t.p1,i.p2=t.p2,o(),l.A(t.p1);let n=l._();s[n]||(s[n]=!0,e.push(n))}i.p1=r,i.p2=c,n()}}return e}_(){return this.R.sort(((t,e)=>t.k!=e.k?t.k-e.k:t.h()<e.h()?-1:1)).map((t=>t.h())).join("\n")}I(t){let e=t.split("\n").map((t=>r.g(t)));this.R=e}static M(t){let s=e.from(t).map(c.V),[i,o,l,h,a,f]=s,u=new n(l,h),d=new n(a,f),p=[];for(let t=6;t<s.length-3;t+=4){let[e,i,o,c]=s.slice(t,t+4);p.push(new r(new n(e,i),new n(o,c),!1))}return{width:i,height:o,start:u,end:d,R:p}}static q(t){let e=[],s=0,i=0,r=[],l=[];for(;s<t.length-1;){let h=c.V(t[s]);for(let a=0;a<h&&s+1+a<t.length;a++){let h=c.V(t[s+1+a]),f=new n(h,i);if(e.push(f),l[h]||(l[h]=[]),l[h].push(f),a>0){let t=new o(e[e.length-2],f);r.push(t)}}s+=h+1,i++}for(const t of l)if(t&&t.length>1)for(let e=1;e<t.length;e++){let s=new o(t[e-1],t[e]);r.push(s)}return{i:e,T:r}}static B(t,e){let{width:s,height:i,start:n,end:o,R:r}=c.M(t),{i:l,T:h}=c.q(e);return new c(s,i,l,n,o,h,r)}static D="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";static V(t){return c.D.indexOf(t)}static K(t){return c.D[t]}static O={Node:"node",G:"stick",J:"edge"};L(t,e,i,n,o){let{width:r,height:c}=this,l=s.floor(s.min(n/r,o/c)),h=.15*l,a=.15*l,f=s.floor(l/2)-.5,u=f;this.U={Z:t,x:e,y:i,width:n,height:o,j:l,tt:h,et:a,lineWidth:1,offsetX:f,offsetY:u}}st(){let{width:t,height:e,i,start:n,end:o,R:r}=this,{Z:c,x:l,y:h,width:a,height:f,j:u,tt:d,et:p,lineWidth:b,offsetX:g,offsetY:m}=this.U;c.save(),c.clearRect(l,h,a,f),c.translate(g,m);let w=(t,e)=>{c.moveTo(t.x*u,t.y*u),c.lineTo(e.x*u,e.y*u)},S=t=>{if(c.strokeStyle=t.v?t.C?`rgba(${[255,203,13,.3]})`:"#FFCB0D":"#960",c.beginPath(),w(t.p1,t.p2),c.stroke(),t.C){c.lineWidth=b,c.strokeStyle="#FFC800",c.beginPath();let{x:e,y:s}=t.p1,{x:i,y:n}=t.p2,o=0,r=0;t.u?r=(p/2-b/2)/u:o=(p/2-b/2)/u,w({x:e+o,y:s+r},{x:i+o,y:n+r}),w({x:e-o,y:s-r},{x:i-o,y:n-r}),c.stroke(),c.strokeStyle="#666",c.lineWidth=b,c.beginPath(),w(t.p1,t.p2),c.stroke(),c.lineWidth=p}},k=t=>{c.beginPath(),c.ellipse(u*t.x,u*t.y,d,d,0,0,2*s.PI),c.fill(),c.stroke()};c.strokeStyle="#666",c.lineWidth=b,c.beginPath();for(let s=1;s<t-1;s++)w({x:s,y:0},{x:s,y:e-1});for(let s=0;s<e;s++)w({x:1,y:s},{x:t-2,y:s});w(n,{x:n.x+1,y:n.y}),w(o,{x:o.x-1,y:o.y}),c.stroke(),c.lineWidth=p;for(const t of r)S(t);c.strokeStyle="#000",c.lineWidth=2,c.fillStyle="#03a9f4";for(const t of i)t.l(n)||t.l(o)||k(t);c.fillStyle="#f60",k(n),c.fillStyle="#6c6",k(o),c.restore()}it(t,e){let{j:i,tt:o,et:r,offsetX:l,offsetY:h}=this.U,a=new n(t-=l,e-=h);for(const t of this.i){if(new n(t.x*i,t.y*i).o(a)<=o)return{type:c.O.Node,node:t}}let f=(t,e,n)=>{let{p1:o,p2:r,u:c}=e,l=s.min(o.x,r.x)*i,h=s.min(o.y,r.y)*i,a=s.max(o.x,r.x)*i,f=s.max(o.y,r.y)*i;c?(h-=n/2,f+=n/2):(l-=n/2,a+=n/2);let{x:u,y:d}=t;return u>=l&&u<=a&&d>=h&&d<=f};for(const t of this.R)if(f(a,t,r))return{type:c.O.G,nt:t};let u=[];for(const t of this.T)f(a,t,2*r)&&u.push(t);return u.length?{type:c.O.J,T:u}:null}}let l,h,a=(e,s)=>(s||t).querySelector(e),f="8600720030121363653454",u="3036324541257314632353256",d=18,p=0,b=a("canvas#plank-canvas"),g=b.getContext("2d"),m=()=>{p&&(clearInterval(p),p=0)},w=(t,e)=>{m(),f=t,u=e,l=c.B(f,u),h=l._(),l.L(g,0,0,b.width,b.height),l.st()},S=t=>{d=(d+t)%i.length;let e=i[d];w(e.t,e.i),a("#level-number").textContent=d+1+"/"+i.length};b.addEventListener("click",(t=>{m();let e=t.offsetX,s=t.offsetY,i=l.it(e,s);if(i)switch(i.type){case c.O.G:{let{nt:t}=i;t.v&&(l.F(t),l.st());break}case c.O.J:l.W(i.T)&&(l.st(),l.P()&&setTimeout((()=>alert("You win!")),1))}})),a("#btn-solve").addEventListener("click",(t=>{m();let e=function(t,e){if(e=e||t._(),t.P())return{ot:!0,rt:1,ct:[e],lt:0};let i={[e]:null},n=[[e,0]],o=!1,r="",c=0,l=0,h=[];for(;n.length>0;){let[e,s]=n.shift();if(c++,t.I(e),t.P()){if(o=!0,h.push(e),l++,l>1)continue;r=e;continue}if(o)continue;if(c>=1e7)break;let a=t.X();for(const t of a)i.hasOwnProperty(t)||(i[t]=e,n.push([t,s+1]))}if(t.I(e),o){r=h[h.length*s.random()|0];let t=r,n=[t];for(;t!=e;)t=i[t],n.unshift(t);return{ot:o,rt:c,ct:n,lt:n.length-1,ht:l}}return{ot:o,rt:c}}(l,h);if(e.ot){let t=0;p=setInterval((()=>{if(t>=e.ct.length)return void m();let s=e.ct[t++];l.I(s),l.st()}),1e3)}})),a("#btn-reset").addEventListener("click",(t=>{m(),l.I(h),l.st()})),a("#btn-prev-level").addEventListener("click",(()=>S(-1))),a("#btn-next-level").addEventListener("click",(()=>S(1))),w(f,u)}(document,Array,Math);