Last active
December 5, 2016 10:06
-
-
Save fhfaa/3cedcd358bd67e5c87933dad260807a8 to your computer and use it in GitHub Desktop.
Advent of Code 2016
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
// Chrome DevTools console ~~ enter pt.1 & 2 separately | |
// var input = $({auto-generated <pre>-tag w/ input txt}).textContent.trimTrailingNL(); | |
// I.) | |
input.split(', ').reduce(function (o,e,i,a) { | |
o.i=(o.i+(e[0]=='L'?3:5))%4; | |
o[o.i%2]+=(o.i>1?-1:1)*+e.substr(1); | |
return i+1==a.length?Math.abs(o[0])+Math.abs(o[1]):o; | |
},{i:0,0:0,1:0}) | |
input.split(', ').reduce(function (o,e,s,n) { | |
if (typeof o === 'number') return o; | |
o.i=(o.i+(e[0]=='L'?3:5))%4; | |
return new Array(+e.substr(1)).fill(o.i>1?-1:1).every(function (t) { | |
return (o[o.i%2] += t),(o.c.size < o.c.add(o[0] + ',' + o[1]).size) | |
}) ? o: Math.abs(o[0])+Math.abs(o[1]) | |
},{i:0,0:0,1:0,c:new Set(['0,0'])}) | |
// II.) | |
input.split('\n').reduce(function (o,l) { | |
l.split('').forEach(c => o.p = o[c](o.p)); | |
o.c+=''+(o.p+1); | |
return o; | |
}, { | |
p:4, | |
c:'', | |
U:n=>n>2?n-3:n, | |
D:n=>n<6?n+3:n, | |
L:n=>n%3>0?n-1:n, | |
R:n=>n%3<2?n+1:n | |
}).c; | |
var ud = [0,0,2,0,0,4,4,4,0,4,4,4,2]; | |
var lr = [0,0,1,1,0,1,1,1,1,0,1,1,0]; | |
input.split('\n').reduce(function (o,l) { | |
l.split('').forEach(function (cc) {o.p=o[cc](o.p); }); | |
o.c=o.c+''+(o.p + 1).toString(16); | |
return o; | |
}, { | |
p:4, | |
c:'', | |
U:n=>n-ud[n], | |
D:n=>n+ud.slice().reverse()[n], | |
L:n=>n-lr[n], | |
R:n=>n+lr.slice().reverse()[n] | |
}).c.toUpperCase(); | |
// III.) | |
input.split('\n').filter(function (a) { | |
a = a.split(/\s+/).slice(1).map(parseFloat).sort(); | |
return a[0]+a[1]>a[2] && a[1]+a[2]>a[0] && a[0]+a[2]>a[1] | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment