Last active
December 9, 2019 18:39
-
-
Save emplv/11fc9f03343b3d0f5b69b3093119e30b to your computer and use it in GitHub Desktop.
AoC 2019 - day 7
This file contains hidden or 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
| const INPUT = ` | |
| 3,8,1001,8,10,8,105,1,0,0,21,38,55,72,93,118,199,280,361,442,99999,3,9,1001,9,2,9,1002,9,5,9,101,4,9,9,4,9,99,3,9,1002,9,3,9,1001,9,5,9,1002,9,4,9,4,9,99,3,9,101,4,9,9,1002,9,3,9,1001,9,4,9,4,9,99,3,9,1002,9,4,9,1001,9,4,9,102,5,9,9,1001,9,4,9,4,9,99,3,9,101,3,9,9,1002,9,3,9,1001,9,3,9,102,5,9,9,101,4,9,9,4,9,99,3,9,101,1,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,99,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,99,3,9,101,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,99,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,99,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,99 | |
| `.trim().split(','); | |
| // instruction handler, using generator function to yield on io | |
| Z=([...t])=>function*z(){ | |
| for( | |
| let u,i=0; | |
| [q,w,e,r]=t.slice(i),99-([a,b,c,_,u]=(q+'').padStart(5,0)); | |
| i+=u%7<3?4:u<5?2:u<6^!+x?+y-i:3 | |
| ){ | |
| x=+c?w:t[w]; | |
| y=+b?e:t[e]; | |
| u-3?u-4?t[[,r,r][u%6]]=[x==y,x-+-y,x*y,x<y][u%4]:yield x:t[w]=yield; | |
| } | |
| } | |
| // create all permutations | |
| P=A=>(r=[],p=(arr,m=[])=>(0 in arr?(_=>{for(let i=0;i in arr;i++)p(c=[...arr],[...m,...c.splice(i,1)])})():r.push(m),r))(A); | |
| // part 1: 368584 | |
| p1=I=>P([0,1,2,3,4]).map(P=> | |
| P.map(p=>{ | |
| let a=Z(I)(); | |
| a.next(); | |
| return{a,s:a.next(p)} | |
| },O=0).map(a=>O=a.a.next(O).value)|O | |
| ).reduce((p,c)=>p>c?p:c,0); | |
| // part 2: 35993240 | |
| p2=I=>P([9,8,7,6,5]).map(P=>( | |
| A=P.map(p=>{ | |
| let a=Z(I)(); | |
| a.next(); | |
| return{a,s:a.next(p)} | |
| },O=0), | |
| F=i=>A[4].s.done?O:F((O=A[i].a.next(O).value,A[i].s=A[i].a.next(),++i%5)) | |
| )(0) | |
| ).reduce((p,c)=>p>c?p:c,0); | |
| // no whitespaces: | |
| Z=([...t])=>function*z(){for(let u,i=0;[q,w,e,r]=t.slice(i),99-([a,b,c,_,u]=(q+'').padStart(5,0));i+=u%7<3?4:u<5?2:u<6^!+x?+y-i:3){x=+c?w:t[w];y=+b?e:t[e];u-3?u-4?t[[,r,r][u%6]]=[x==y,x-+-y,x*y,x<y][u%4]:yield x:t[w]=yield}} | |
| P=A=>(r=[],p=(arr,m=[])=>(0 in arr?(_=>{for(let i=0;i in arr;i++)p(c=[...arr],[...m,...c.splice(i,1)])})():r.push(m),r))(A) | |
| p1=I=>P([0,1,2,3,4]).map(P=>P.map(p=>{let a=Z(I)();a.next();return{a,s:a.next(p)}},O=0).map(a=>O=a.a.next(O).value)|O).reduce((p,c)=>p>c?p:c,0) | |
| p2=I=>P([9,8,7,6,5]).map(P=>(A=P.map(p=>{let a=Z(I)();a.next();return{a,s:a.next(p)}},O=0),F=i=>A[4].s.done?O:F((O=A[i].a.next(O).value,A[i].s=A[i].a.next(),++i%5)))(0)).reduce((p,c)=>p>c?p:c,0) | |
| // | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment