Skip to content

Instantly share code, notes, and snippets.

@magne4000
Last active December 17, 2020 14:41
Show Gist options
  • Save magne4000/3c39d7f79d4063ffac06c0501b6e5e74 to your computer and use it in GitHub Desktop.
Save magne4000/3c39d7f79d4063ffac06c0501b6e5e74 to your computer and use it in GitHub Desktop.
One liner answers in JS to https://adventofcode.com/2020

Those scripts can be run on the input page, directly from the devtool console.

Day 1: Report Repair

Part 1

v=document.querySelector('pre').innerHTML.split('\n').map(x => parseInt(x, 10)).filter(x => !isNaN(x));v.forEach(x=>{v.forEach(y=>{if(x+y===2020)r=x*y})});r

Part 2

v=document.querySelector('pre').innerHTML.split('\n').map(x => parseInt(x, 10)).filter(x => !isNaN(x));v.forEach(z=>{v.forEach(x=>{v.forEach(y=>{if(x+y+z===2020)r=x*y*z})})});r

Day 2: Password Philosophy

Part 1

document.querySelector('pre').innerHTML.split('\n').map(line => line.match(/(\d+)\-(\d+) ([a-z]): (\w+)/)).filter(Boolean).map(line => ((line[4].match(new RegExp(line[3], 'g'))?.length ?? 0) >= parseInt(line[1])) && ((line[4].match(new RegExp(line[3], 'g'))?.length ?? 0) <= parseInt(line[2]))).filter(Boolean).length

Part 2

document.querySelector('pre').innerHTML.split('\n').map(line => line.match(/(\d+)\-(\d+) ([a-z]): (\w+)/)).filter(Boolean).map(line => (line[4][parseInt(line[1])-1] === line[3]) ^ (line[4][parseInt(line[2])-1] === line[3])).filter(Boolean).length

Day 3: Toboggan Trajectory

Part 1

x=y=c=0;l=document.querySelector('pre').innerHTML.split('\n').filter(Boolean);while(y<l.length-1){x=(x+3)%l[0].length;y=y+1;l[y][x]=='#'&&c++};c

Part 2

l=document.querySelector('pre').innerHTML.split('\n').filter(Boolean);a=1;for([xx,yy]of[[1,1],[3,1],[5,1],[7,1],[1,2]]){x=y=c=0;while(y<l.length-1){x=(x+xx)%l[0].length;y=y+yy;l[y][x]=='#'&&c++};a*=c};a
// or with a reduce
l=document.querySelector('pre').innerHTML.split('\n').filter(Boolean);[[1,1],[3,1],[5,1],[7,1],[1,2]].reduce((a,[xx,yy])=>{x=y=c=0;while(y<l.length-1){x=(x+xx)%l[0].length;y=y+yy;l[y][x]=='#'&&c++};return a*=c},1)

Day 4: Passport Processing

Part 1

m=['byr','iyr','eyr','hgt','hcl','ecl','pid'];document.querySelector('pre').innerHTML.split('\n\n').filter(Boolean).map(p=>p.split(/\s+/)).map(p=>m.filter(x=>!p.map(i=>i.split(':')[0]).includes(x))).filter(x=>!x.length).length;

Part 2

f=(a,b)=>x=>{y=parseInt(x);return y>=a&&y<=b};m={'byr':f(1920,2002),'iyr':f(2010,2020),'eyr':f(2020,2030),'hgt':x=>{h=x.match(/(\d+)(cm|in)/);return h&&((h[2]==='in'&&f(59,76)(h[1]))||(h[2]==='cm'&&f(150,193)(h[1])))},'hcl':x=>Boolean(x.match(/^#[0-9a-f]{6}$/)),'ecl':x=>['amb','blu','brn','gry','grn','hzl','oth'].includes(x),'pid':x=>Boolean(x.match(/^\d{9}$/))};document.querySelector('pre').innerHTML.split('\n\n').filter(Boolean).map(p=>p.split(/\s+/)).filter(p=>!Object.keys(m).filter(x=>!p.map(i=>i.split(':')[0]).includes(x)).length).filter(p=>p.map(q=>{s=q.split(':');return !(s[0] in m)||m[s[0]](s[1])}).every(Boolean)).length;

Day 5: Binary Boarding

Part 1

f=([a,b])=>((b-a)/2)+a;Math.max(...document.querySelector('pre').innerHTML.split('\n').map(l=>Array.from(l).reduce((a,c,i)=>i<7?[(c=='F'?[a[0][0],Math.floor(f(a[0]))]:[Math.ceil(f(a[0])),a[0][1]]),a[1]]:[a[0],(c=='L'?[a[1][0],Math.floor(f(a[1]))]:[Math.ceil(f(a[1])),a[1][1]])],[[0,127],[0,7]])).map(l=>(l[0][0]*8)+l[1][0]))

Part 2

f=([a,b])=>((b-a)/2)+a;v=document.querySelector('pre').innerHTML.split('\n').filter(Boolean);w=v.map(l=>Array.from(l).reduce((a,c,i)=>i<7?[(c=='F'?[a[0][0],Math.floor(f(a[0]))]:[Math.ceil(f(a[0])),a[0][1]]),a[1]]:[a[0],(c=='L'?[a[1][0],Math.floor(f(a[1]))]:[Math.ceil(f(a[1])),a[1][1]])],[[0,127],[0,7]])).map(l=>(l[0][0]*8)+l[1][0]);v.map((x,i)=>[x,w[i]]).sort((a,b)=>a[1]-b[1]).find((x,i,a)=>x[1]==a[i+1][1]-2)[1]+1

Day 6: Custom Customs

Part 1

document.querySelector('pre').innerHTML.split('\n\n').filter(Boolean).map(p=>new Set(p.split(/\s+/).join(''))).map(s=>s.size).reduce((a,b)=>a+b,0)

Part 2

document.querySelector('pre').innerHTML.split('\n\n').filter(Boolean).map(p=>p.split(/\s+/).filter(Boolean)).map(p=>p.reduce((a,c)=>Array.from(a).filter(e=>c.includes(e)),p[0])).map(s=>s.length).reduce((a,b)=>a+b,0)

Day 7: Handy Haversacks

Part 1

g=document.querySelector('pre').innerHTML.split('\n').filter(Boolean).map(l=>Array.from(l.matchAll(/(.*?) (?:bags? contain )?\d+ (.*?) bags?/g)).map(a=>a.slice(1).filter(s=>s!==',')).flat());c=new Set();p=['shiny gold'];while(p.length){z=g.filter(x=>p.some(y=>x.slice(1).includes(y)));p=z.map(x=>x[0]);p.forEach(x=>c.add(x))};c.size

Part 2

g=document.querySelector('pre').innerHTML.split('\n').filter(Boolean).map(l=>Array.from(l.matchAll(/(.*?) (?:bags? contain )?(\d+) (.*?) bags?/g)).map(a=>a.slice(1).filter(s=>s!==',')).flat());f=k=>{let r=g.find(x=>x[0]==k);if(!r) return 0;let j=r.length-1,t=0;while((j-=2)>=0){t+=parseInt(r[j+1])+(parseInt(r[j+1])*f(r[j+2]))};return t};f('shiny gold')

Day 8: Handheld Halting

Part 1

g=document.querySelector('pre').innerHTML.split('\n').filter(Boolean).map(s=>s.split(' ')).map(([a,b])=>[a,parseInt(b)]);v=new Set();c=0;n=0;while(!v.has(n)){v.add(n);g[n][0]=='nop'?n++:g[n][0]=='jmp'?n+=g[n][1]:(c+=g[n][1],n++)};c

Part 2

g=document.querySelector('pre').innerHTML.split('\n').filter(Boolean).map(s=>s.split(' ')).map(([a,b])=>[a,parseInt(b)]);f=(p)=>{v=new Set();c=0;n=0;b=false;while(!v.has(n)&&!b){nop=p==n?'jmp':'nop';jmp=p==n?'nop':'jmp';v.add(n);if(!g[n]){b=true;break};g[n][0]==nop?n++:g[n][0]==jmp?n+=g[n][1]:(c+=g[n][1],n++)};return [b,c]};m=null;i=0;while(true){let [h,r]=f(i++);m=r;if(h)break;};m

Day 9: Encoding Error

Part 1

g=document.querySelector('pre').innerHTML.split('\n').filter(Boolean).map(x=>parseInt(x));h=g.reduce((a,_,i,o)=>i+25>o.length?a:a.concat([o.slice(i, i+25)]),[]).filter(a=>a.length==25).map(x=>x.map((c,i,o)=>o.filter(x=>x!=c).map(x=>x+c)).flat());n=25;while(h[n-25].includes(g[n])){n++};g[n]

Part 2

g=document.querySelector('pre').innerHTML.split('\n').filter(Boolean).map(x=>parseInt(x));h=g.reduce((a,_,i,o)=>i+25>o.length?a:a.concat([o.slice(i, i+25)]),[]).filter(a=>a.length==25).map(x=>x.map((c,i,o)=>o.filter(x=>x!=c).map(x=>x+c)).flat());n=25;while(h[n-25].includes(g[n])){n++};f=x=>x.reduce((a,b)=>a+b,0);ll=g.reduce((a,c)=>g[n]==f(a)?a:g[n]>f(a)?[...a,c]:a.reduce(u=>f(u)<=g[n]?u:u.slice(1),[...a,c]),[]);Math.min(...ll)+Math.max(...ll)

Day 10: Adapter Array

Part 1

g=document.querySelector('pre').innerHTML.split('\n').filter(Boolean).map(x=>parseInt(x)).sort((a,b)=>a-b);h=[...g,Math.max(...g)+3].map((c,i)=>c-(g[i-1]??0));h.filter(x=>x==1).length*h.filter(x=>x==3).length

Part 2

g=document.querySelector('pre').innerHTML.split('\n').filter(Boolean).map(x=>parseInt(x)).sort((a,b)=>a-b);h=[...g,Math.max(...g)+3].map((c,i)=>c-(g[i-1]??0));h.reduce((a,c)=>c==3?[...a,0]:(a[a.length-1]++,a),[0]).filter(x=>x>1).map(x=>Math.pow(2,x-1)-Math.max(x-3,0)).reduce((a,c)=>a*c,1)

Day 11: Seating System

Part 1

h=g=document.querySelector('pre').innerHTML.split('\n').filter(Boolean);l=g[0].length;xy=i=>[Math.floor(i/l),i%l];k=(x,y)=>g[x]?g[x][y]=='O'?1:0:0;f=i=>([x,y]=xy(i),k(x-1,y-1)+k(x-1,y)+k(x-1,y+1)+k(x,y-1)+k(x,y+1)+k(x+1,y-1)+k(x+1,y)+k(x+1,y+1));do{t=l*g.length;g=h;h=[...g.map(x=>Array.from(x))];while(--t>=0){let [x,y]=xy(t);h[x][y]=(g[x][y]=='O'&&f(t)>3)?'L':(g[x][y]=='L'&&f(t)==0)?'O':g[x][y]};}while(JSON.stringify(g)!=JSON.stringify(h));h.flat().filter(x=>x=='O').length

Part 2

h=g=document.querySelector('pre').innerHTML.split('\n').filter(Boolean);l=g[0].length;xy=i=>[Math.floor(i/l),i%l];k=(x,y,d,e)=>g[x+d]&&g[x+d][y+e]?g[x+d][y+e]=='O'?1:g[x+d][y+e]=='L'?0:k(x+d,y+e,d,e):0;f=i=>([x,y]=xy(i),k(x,y,-1,-1)+k(x,y,-1,0)+k(x,y,-1,1)+k(x,y,0,-1)+k(x,y,0,1)+k(x,y,1,-1)+k(x,y,1,0)+k(x,y,1,1));do{t=l*g.length;g=h;h=[...g.map(x=>Array.from(x))];while(--t>=0){let [x,y]=xy(t);h[x][y]=(g[x][y]=='O'&&f(t)>4)?'L':(g[x][y]=='L'&&f(t)==0)?'O':g[x][y]};}while(JSON.stringify(g)!=JSON.stringify(h));h.flat().filter(x=>x=='O').length

Day 12: Rain Risk

Part 1

document.querySelector('pre').innerHTML.split('\n').filter(Boolean).map(l=>[l[0],parseInt(l.slice(1))]).reduce(([x,y,i],[d,n])=>{d=d=='F'?['E','S','W','N'][i]:d;d=='L'?i=(i+4-(n/90))%4:d=='R'?i=(i+(n/90))%4:d=='N'?y-=n:d=='S'?y+=n:d=='W'?x-=n:d=='E'?x+=n:null;return [x,y,i]},[0,0,0]).reduce((a,b)=>Math.abs(b)+a,0)

Part 2

document.querySelector('pre').innerHTML.split('\n').filter(Boolean).map(l=>[l[0],parseInt(l.slice(1))]).reduce(([x,y,wx,wy],[d,n])=>{i=d=='L'?(4-(n/90))%4:d=='R'?(n/90)%4:0;while(i-->0){t=wx;wx=-wy;wy=t};d=='N'?wy-=n:d=='S'?wy+=n:d=='W'?wx-=n:d=='E'?wx+=n:null;d=='F'?(x+=n*wx,y+=n*wy):null;return [x,y,wx,wy]},[0,0,10,-1]).slice(0,2).reduce((a,b)=>Math.abs(b)+a,0)

Day 13: Shuttle Search

Part 1

g=document.querySelector('pre').innerHTML.split('\n').filter(Boolean);t=parseInt(g[0]);b=g[1].split(',').filter(v=>v!='x').map(v=>parseInt(v));m=b.map(v=>Math.ceil(t/v)*v);b[m.findIndex(x=>x==Math.min(...m))]*(Math.min(...m)-t)

Part 2

b=document.querySelector('pre').innerHTML.split('\n').filter(Boolean)[1].split(',').map((v,i)=>v=='x'?[1,i]:[parseInt(v),i]).filter(v=>v[0]>1);h=b.map(([a,u])=>[a,u]);h.reduce(([r,m],c)=>{while((r+c[1])%c[0]!=0){r+=m};m*=c[0];return [r,m]},[1,1])[0]

Day 14: Docking Data

Part 1

Object.values(document.querySelector('pre').innerHTML.split('\n').filter(Boolean).map(v=>v.match(/(mask) = (.*)|(mem)\[(\d+)\] = (\d+)/)).map(v=>v[1]?[v[2]]:[v[4],v[5]]).reduce(([ma0,ma1,me],c)=>{if(c.length==1)return [BigInt(parseInt(c[0].replace(/X/g,'1'),2)),BigInt(parseInt(c[0].replace(/X/g,'0'),2)),me];return [ma0,ma1,{...me,[c[0]]:BigInt(parseInt(c[1]))&ma0|ma1}]},[0n,0n,{}])[2]).reduce((a,c)=>a+c,0n)

Part 2

g=document.querySelector('pre').innerHTML.split('\n').filter(Boolean);let mask,mem={},alts=[];for(let t of g){let e=t.split(" = ")[0],s=t.split(" = ")[1];if("mask"!=e){for(e=parseInt(e.substr(4)).toString(2).split(""),s=parseInt(s);e.length<36;)e.unshift(0);for(let t=0;t<36;t++)"0"!=mask[t]&&("1"==mask[t]&&(e[t]=1),"X"==mask[t]&&(e[t]=0));e=parseInt(e.join().replaceAll(",",""),2);for(let t of alts)mem[e+t]=s}else{mask=s,alts=[0];for(let t=0;t<36;t++)if("X"==mask[t]){let e=[];for(let s of alts)e.push(2**(35-t)+s);alts=alts.concat(e)}}};Object.values(mem).reduce((a,b)=>a+b,0)

Day 15: Rambunctious Recitation

Part 1

g=document.querySelector('.puzzle-input').innerHTML.split(',').map(x=>parseInt(x));l=0;for(t=g.length;t<2020;t++){i=[...g].reverse().indexOf(l);i=i==-1?0:t+1-g.length+i;g.push(l);l=i};g[g.length-1]

Part 2

g=new Map(document.querySelector('.puzzle-input').innerHTML.split(',').map(x=>parseInt(x)).map((v,j)=>[v,j]));l=0;for(t=g.size-1;t<30000000-2;t++){i=g.get(l);i=i==null?0:t+1-i;g.set(l,t+1);l=i};l

Day 16: Ticket Translation

Part 1

[ru,,ne]=document.querySelector('pre').innerHTML.split('\n\n');ru=ru.split('\n').map(v=>v.match(/(.*): (\d+)-(\d+) or (\d+)-(\d+)/)).map(([,a,b,c,d,e])=>[a,parseInt(b),parseInt(c),parseInt(d),parseInt(e)]);ne=ne.split('\n').filter(Boolean);ne.shift();ne=ne.map(v=>v.split(',').map(v=>parseInt(v)));f=x=>ru.some(([,a,b,c,d])=>(x>=a&&x<=b)||(x>=c&&x<=d));ne.flat().reduce((a,c)=>a+(f(c)?0:c),0)

Part 2

[ru,my,ne]=document.querySelector('pre').innerHTML.split('\n\n');ru=ru.split('\n').map(v=>v.match(/(.*): (\d+)-(\d+) or (\d+)-(\d+)/)).map(([,a,b,c,d,e])=>[a,parseInt(b),parseInt(c),parseInt(d),parseInt(e)]).map(([a,b,c,d,e])=>[a,x=>(x>=b&&x<=c)||(x>=d&&x<=e)]);my=my.split('\n')[1].split(',').map(v=>parseInt(v));ne=ne.split('\n').filter(Boolean);ne.shift();ne=ne.map(v=>v.split(',').map(v=>parseInt(v)));g=x=>ru.some(([,fn])=>fn(x));ne=ne.filter(v=>v.every(g));h=ne.map(v=>v.map(x=>ru.map(([_,f],i)=>f(x)?i:-1).filter(x=>x!==-1)));p=ru.map((_,j)=>h.map(v=>v[j]).reduce((a,b)=>a.filter(v=>b.includes(v)),ru.map((_,i)=>i)));t=p.length;while(t-->0){p=p.map(l=>l.length===1?l:l.filter(x=>!p.filter(y=>y.length===1).map(y=>y[0]).includes(x)))};p=p.map(v=>v[0]);o=ru.map(([v],i)=>v.startsWith('departure')?p.indexOf(i):null).filter(v=>v!==null);o.map(v=>my[v]).reduce((a,b)=>a*b,1)

Day 17: Conway Cubes

Part 1

n=Array(27).fill(0).map((_,i)=>[(i%3)-1,(Math.floor(i/3)%3)-1,Math.floor(i/9)-1]).filter(([x,y,z])=>x|y|z!==0);m=new Map([new Map(document.querySelector('pre').innerHTML.split('\n').filter(Boolean).map(v=>new Map(Array.from(v).map(x=>x=='#'?1:0).map((v,i)=>[i,v]))).map((v,i)=>[i,v]))].map((v,i)=>[i,v]));f=(x,y,z)=>n.reduce((a,c)=>a+(m.get(z+c[2])?.get(y+c[1])?.get(x+c[0])??0),0);g=(x,y,z,ar)=>n.forEach(c=>m.get(z+c[2])?.get(y+c[1])?.get(x+c[0])??ar.push([x+c[0],y+c[1],z+c[2],0]));p=a=>{for([x,y,z,v] of a){if(!m.has(z))m.set(z,new Map());if(!m.get(z).has(y))m.get(z).set(y,new Map());m.get(z).get(y).set(x,v)}};q=ar=>{for([z,zv] of m){for([y,yv] of zv){for ([x,xv] of yv){g(x,y,z,ar)}}}};for(t=0;t<6;t++){a=[];q(a);p(a);a=[];for([z,zv] of m){for([y,yv] of zv){for ([x,xv] of yv){fr=f(x,y,z);if((xv&&(fr<2||fr>3))||(!xv&&fr==3))a.push([x,y,z,xv?0:1]);}}};p(a)};Array.from(m.values()).reduce((z,zc)=>z+Array.from(zc.values()).reduce((y,yc)=>y+Array.from(yc.values()).reduce((x,xc)=>x+xc,0),0),0)

Part 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment