Skip to content

Instantly share code, notes, and snippets.

@fhfaa
Last active December 5, 2016 10:06
Show Gist options
  • Save fhfaa/af5dda361ac0300909ef to your computer and use it in GitHub Desktop.
Save fhfaa/af5dda361ac0300909ef to your computer and use it in GitHub Desktop.
Advent of Code 2015
// Chrome DevTools console ~~ enter pt.1 & 2 separately
// var input = $({auto-generated <pre>-tag w/ input txt}).textContent.trimTrailingNL();
// I.)
input.split('').reduce((e,s) => e+(s==='('?1:-1),0)
input.split('').some(((p) => (c,i) => !~(p+=c==='('?1:-1)?console.log(++i)|1:0)(0))
// II.)
input.split('\n').map((s) => s.split('x').map(parseFloat).sort((a,b) => a-b)).map((a) => a[0]*a[1]*3+a[1]*a[2]*2+a[0]*a[2]*2).reduce((a,b) => a+b)
input.split('\n').map((s) => s.split('x').map(parseFloat).sort((a,b) => a-b)).map((a) => (a[0]+a[1])*2+a[0]*a[1]*a[2]).reduce((a,b) => a+b)
// III.)
input.split('').reduce((d,c,n) => (({'^':() => d.y--,'v':() => d.y++,'<':() => d.x--,'>':() => d.x++})[c]())&(!~d.m.indexOf(n=d.x+','+d.y)?d.m.push(n)|d.z++:0)&0||d,{x:0,y:0,z:1,m:['0,0']}).z
input.split('').reduce((d,c,n,w) => (w=d.p[+!(n%2)])&({'^':() => w.y--,'v':() => w.y++,'<':() => w.x--,'>':() => w.x++})[c]()&(!~d.m.indexOf(n=w.x+','+w.y)?d.m.push(n)|d.z++:0)&0||d,{p:[{x:0,y:0},{x:0,y:0}],z:1,m:['0,0']}).z
// IV.) ~ assume imported md5(String):String
for(var i=1;md5(input+i).indexOf('00000')!==0;i++);console.log(i);
for(var i=1;md5(input+i).indexOf('000000')!==0;i++);console.log(i);
// V.)
input.split('\n').filter((s) => s.replace(/[^aeiou]/gi,'').length>2 &&/(.)\1/.test(s)&&!(/ab|cd|pq|xy/).test(s)).length
input.split('\n').filter((s) => /(..).*\1/.test(s) && /(.).\1/.test(s)).length
// VI.)
input.split('\n').map((s) => s.match(/([enf]) (\d+),(\d+)\D+(\d+),(\d+)/)).map((a) => [a[1]].concat(a.slice(2).map(parseFloat))).reduce((a,x) => new Array(x[3]-x[1]+1).fill().forEach((e,i) => (i+=x[1])|new Array(x[4]-x[2]+1).fill().forEach((e,j) => (j += x[2])|(a[i+1000*j]=x[0]==='e'&&a[i+1000*j]||x[0]==='f'?0:1)))||a, new Int8Array(1e6)).reduce((a,b) => a+b)
input.split('\n').map((s) => s.match(/([enf]) (\d+),(\d+)\D+(\d+),(\d+)/)).map((a) => [a[1]].concat(a.slice(2).map(parseFloat))).reduce((a,x) => new Array(x[3]-x[1]+1).fill().forEach((e,i) => (i+=x[1])|new Array(x[4]-x[2]+1).fill().forEach((e,j) => (j+=x[2])|(a[i+1000*j]+=x[0]==='e'?2:(x[0]==='n'?1:(a[i+1000*j]?-1:0)))))||a, new Int8Array(1e6)).reduce((a,b) => a+b)
// VII.)
/*const RESULT_PT1 = */(input).split('\n').map((s) => s.match(/^(?:(?:(\w+) )?([A-Z]+) )?(\w+) -> (\w+)/)).reduce((dat,x) => (dat[x[4]]=x)&&dat,{_:function($){return !this[$]?$:this[$].val!==undefined?this[$].val:(this[$].val=({'LSHIFT':(a,b) => a<<b,'RSHIFT':(a,b) => a>>b,'AND':(a,b) => a&b,'NOT':(a,b) => ~b,'OR':(a,b) => a|b,'undefined':(a,b) => b})[this[$][2]](this._(this[$][1]),this._(this[$][3])))}})._('a')
(input+'\n'+RESULT_PT1+' -> b').split('\n').map((s) => s.match(/^(?:(?:(\w+) )?([A-Z]+) )?(\w+) -> (\w+)/)).reduce((dat,x) => (dat[x[4]]=x)&&dat,{_:function($){return !this[$]?$:this[$].val!==undefined?this[$].val:(this[$].val=({'LSHIFT':(a,b) => a<<b,'RSHIFT':(a,b) => a>>b,'AND':(a,b) => a&b,'NOT':(a,b) => ~b,'OR':(a,b) => a|b,'undefined':(a,b) => b})[this[$][2]](this._(this[$][1]),this._(this[$][3])))}})._('a')
// VIII.)
input.split('\n').reduce((a,s) => a+s.length-eval(s).length,0);
input.split('\n').reduce((a,s) => a-s.length+uneval(s).length,0); // Firefox only
// IX.) 2-in-1
((d,q) => (q=(x,e,i,a) => x.concat(a.length>1&&a.slice(0,i).concat(a.slice(i+1)).reduce(q,[]).map((p) => [e].concat(p))||e)) && Object.keys(d).reduce(pp,[]).map((a) => a.reduce((x,e,i,a) => i ? x + d[a[i - 1]][a[i]]:0,0)).sort((a,b) => a-b).filter((e,i,a) => !i||i===a.length-1))(input.split('\n').map((s) => (s).match(/(\w+) to (\w+) = (\d+)/)).reduce((o,a) => ((o[a[1]]||(o[a[1]]={}))[a[2]]=(o[a[2]]||(o[a[2]]={}))[a[1]]=+a[3])&&o,{}))
// X.)
for (var s=input,i=0;i<40; s=s.replace(/(\d)\1*/g,(d) => d.length+d[0]),i++);s.length;
for (var s=input,i=0;i<50; s=s.replace(/(\d)\1*/g,(d) => d.length+d[0]),i++);s.length;
// XI.) 2-in-1
((p,test,inc) => eval("for(var i=0;inc(p)&&(!test(p)||console.log(p.map((n) => String.fromCharCode(n&255)).join(''))||(i++==0)););"))(input.split('').map((s) => s.charCodeAt(0)),(a,ix) => a.every((e) => !~[105,108,111].indexOf(e)) && a.slice(2).some((e,i,a) => a[i-2]+2==e&&a[i-1]+1===e)&&a.slice(1).some((e,i,a) => (ix=i+2)&&a[i-1]===e)&&a.slice(ix).some((e,i,a) => a[i-1]===e),((pp) => (pp=(((a,i) => (i===undefined?(i=a.length-1)&0:0)||(a[i]<122?a[i]++:(a[i]=97)|pp(a, i-1)))))||pp)());
// XII.)
for (var sum=0;!JSON.stringify(JSON.parse(input),(k,v) => ((sum+=(typeof v==='number'?v:0)),0)||v),console.log(sum););
for (var sum=0;!JSON.stringify(JSON.parse(input),(k,v) => ((sum+=(typeof v==='number'?v:0)),0)||!Array.isArray(v) && Object.keys(v).some((p) => v[p]==='red')?0:v),console.log(sum););
// bzw pt.1
input.match(/-?\d+/g).map(parseFloat).reduce((a,b) => a+b)
// XIII.)
((k,q,d) => (k={})&(d=input.replace(/ lose /g,' -').split('\n').map((s) => s.match(/^(\w+) .*?(-?\d+).*? (\w+)\.$/)).reduce((s,a) => ((k[a[1]]=1)&(s[a[1]+a[3]]=s[a[3]+a[1]]=((s[a[1]+a[3]]||s[a[3]+a[1]])||0)+(+a[2]))&0)||s,{}))&(q=(x,e,i,a) => x.concat(a.length>1 &&a.slice(0,i).concat(a.slice(i+1)).reduce(q,[]).map((p) => [e].concat(p))||e))&0||Object.keys(k).reduce(q,[]).map((a) => a.map((e,i,a) => d[e+(a[i+1]||a[0])]||0).reduce((a,b) => a+b)).sort((a,b) => b-a)[0])()
((k,q,d) => (k={me:1})&(d=input.replace(/ lose /g,' -').split('\n').map((s) => s.match(/^(\w+) .*?(-?\d+).*? (\w+)\.$/)).reduce((s,a) => ((k[a[1]]=1)&(s[a[1]+a[3]]=s[a[3]+a[1]]=((s[a[1]+a[3]]||s[a[3]+a[1]])||0)+(+a[2]))&0)||s,{}))&(q=(x,e,i,a) => x.concat(a.length>1 &&a.slice(0,i).concat(a.slice(i+1)).reduce(q,[]).map((p) => [e].concat(p))||e))&0||Object.keys(k).reduce(q,[]).map((a) => a.map((e,i,a) => d[e+(a[i+1]||a[0])]||0).reduce((a,b) => a+b)).sort((a,b) => b-a)[0])()
// XIV.)
input.split('\n').map((s) => (/(\d+)\D+(\d+)\D+?(\d+)/g).exec(s).map(parseFloat)).map((a,n) => (n=Math.floor(2503/(a[2]+a[3])))*a[1]*a[2] + Math.min(2503-n*(a[2]+a[3]),a[2])*a[1]).sort((a,b) => b-a)[0]
// 2503 Iterations If moving add velocity to dist | If no more steps toggle rest/move &set steps Sort by total distance each iter Give points to leader(s) END | INPUT Velocity, movemenT steps, total Distance, Rest steps, Points, Moving?, cur steps
new Array(2503).fill().reduce((a) => a.forEach((r) => (r.d+=r.m?r.v:0)|((--r.i)||(r.i=(r.m=!r.m)?r.t:r.r))&0||r)||(a.sort((a,b) => b.d-a.d).every((e,i,a) => a[i].d===a[0].d?++e.p:0)&0)||a,input.split('\n').map((s) => (/(\d+)\D+(\d+)\D+?(\d+)/g).exec(s)).map((a) => ({v:+a[1],t:+a[2],d:0,r:+a[3],p:0,m:!0,i:+a[2]}))).sort((a,b) => b.p-a.p)[0].p
// XV.) 2-in-1
// for i := 0..100 first ingred. for j := 0..(100-i) 2nd ingred. k := third ingred. l := rest per stat (not calor), get max(0,totalStat), multiply incr pt2.max? incr. pt1.max? sum vars stat calculator (num*stat+num*stat+...) transform input into [/*stat1*/ [ingr1.stat1.val,ingr2.stat1.val], [ingr1.stat2.,ingr2.stat2.val], ...] with calories last initial stat arr
((b,b2,stat,ing) => new Array(100).fill().forEach((e,i) => new Array(100-i).fill().forEach((e,j) => new Array(100-i-j).fill().forEach((e,k,a,t) => (a=[i,j,k,100-i-j-k])|(t=ing.slice(0,4).reduce((p,x) => p*stat(x,a),1))|(t>b2&&stat(ing[4],a)===500?b2=t:0)|(t>b?b=t:0))))||[b,b2])(0,0,(a,a2) => Math.max(a[0]*a2[0]+a[1]*a2[1]+a[2]*a2[2]+a[3]*a2[3],0),input.split('\n').map((s) => s.substr(s.indexOf(':')+2).split(/[^-\d]+/).slice(1).map(parseFloat)).reduce((a,d) => d.forEach((e,y) => a[y].push(e))||a,[[],[],[],[],[]]))
// XVI.) ~ input2 is the plaintext from the MFCSAM tape
(((dat,u) => input.split('\n').map((s,i) => eval('({'+s.replace(/^Sue \d+: /,'id:'+(i+1)+',')+'})')).filter((o) => Object.keys(dat).every((k) => o[k]===u||dat[k]===o[k]))[0].id)(eval('({'+input2.replace(/\n/g,',')+'})')))
(((dat,u) => input.split('\n').map((s,i) => eval('({'+s.replace(/^Sue \d+: /,'id:'+(i+1)+',')+'})')).filter((o) => Object.keys(dat).every((k) => o[k]===u||(/^cat|^t/.test(k)?dat[k]<o[k]:(/^po|^g/.test(k)?dat[k]>o[k]:dat[k]===o[k]))))[0].id)(eval('({'+input2.replace(/\n/g,',')+'})')))
// XVII.) 2-in-1
((c,z,p) => (c={})&(z=0)&(p=(a,i,r,n) => (a[i]===r&&++z&&(c[n]=1))&(r>0&&i<a.length?p(a,i+1,r-a[i],n+1)|p(a,i+1,r,n):0))&p(input.split('\n').map(parseFloat).sort((a,b)=>a-b),0,150,1)&0||[z,Math.min(...Object.keys(c))])()
// XVIII.) 2-in-1
// 100 Iter's. pt.1 array pt.2 array pt.1 pt.2 count 1/ON's when done convert input to 2d array turn [#.] into [10] fix() ~ turn on lights in the corners step() for row y-1..y+2 and col x-1..x+1 count ONs, subtract own val toggle light accordingly
((arr,fix,step) => new Array(100).fill().reduce((s) => [s[0].map(step),fix(s[1].map(step))],[arr,fix(arr)]).map((a) => [].concat(...a).reduce((a,b) => a+b)))(input.split('\n').map((s) => s.split('').map((c) => c==='.'?0:1)),(a) => [[1,...a[0].slice(1,-1),1],...a.slice(1,-1),[1,...a[a.length-1].slice(1,-1),1]],(e,y,ay) => ay[y].map((e,x,ax,sum) => (sum=ay.slice(y?y-1:0,y+2).reduce((n,ax) => n+(ax.slice(x?x-1:0,x+2).reduce((a,b)=>a+b,0)),0)-e)&0||(sum===3||(e&&(sum===2))?1:0)))
// XIX.)
Object.keys(((t,s,r,f) => ((f=(k,p,e,i,a) => ((p=s.indexOf(k,p+1))>-1)?(r[s.substr(0,p)+e+s.substr(p+k.length)]=1)|f(k,p,e,i,a):0)|Object.keys(t).forEach(key => t[key].forEach(f.bind(0,key,0)))||r))(input.split('\n\n')[0].split('\n').map(s => s.split(' => ')).reduce((s,x) => (s[x[0]]||(s[x[0]]=[])).push(x[1])&&s,{}),input.split('\n\n')[1].trim(),{})).length
// TODO
// XX.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment