Skip to content

Instantly share code, notes, and snippets.

@fhfaa
Last active December 12, 2017 12:21
Show Gist options
  • Save fhfaa/42cc1d91c270eeebabd5d8add9463f8e to your computer and use it in GitHub Desktop.
Save fhfaa/42cc1d91c270eeebabd5d8add9463f8e to your computer and use it in GitHub Desktop.
Advent of Code 2017
// Chrome DevTools console ~~ enter pt.1 & 2 separately
// var input = $({auto-generated <pre>-tag w/ input txt}).textContent.trimTrailingNL();
// I.)
input.split('').map((e,i,a)=>e===(a[i+1]||a[0])?+e:0).reduce((a,b)=>a+b)
((t,l)=>(l=t.length/2)&&t.split('').map((e,i,a)=>e==a[(i+l)%t.length]?+e:0).reduce((a,b)=>a+b))(input)
// II.)
input.split('\n').map(r => (r=r.split('\t').map(parseFloat))&&Math.abs(Math.max(...r)-Math.min(...r))).reduce((a,b)=>a+b)
input.split('\n').reduce((s,r,d) => r.split('\t').map(parseFloat).sort((b,a) => a-b).some((e, i ,a) => a.slice(i+1).some(e2 => ((d=e/e2)|0)===d))&&(s+d),0)
// III.)
((x, y, dir, len, tar) => {
for (i = 2; i <= 368078; i++) {
switch (dir) {
case 0: x += 1; break;
case 1: y -= 1; break;
case 2: x -= 1; break;
case 3: y += 1;
}
if (--tar == 0) {
dir = (dir + 1) % 4;
tar = len;
len += dir & 1
}
}
return Math.abs(x) + Math.abs(y);
})(0,0,0,1,1);
((x, y, dir, len, tar, memo, mg, ms) => {
mg = (a, b) => memo[a+'.'+b] || 0;
ms = (a, b) => memo[a+'.'+b] = mg(a-1,b-1) + mg(a-1,b) + mg(a-1,b+1) +
mg(a,b-1) + mg(a,b+1) + mg(a+1,b-1) + mg(a+1,b) + mg(a+1,b+1);
for (i = 2;i <= 368078; i++) {
switch (dir) {
case 0: x += 1; break;
case 1: y -= 1; break;
case 2: x -= 1; break;
case 3: y += 1;
}
if (ms(x,y) > 368078) {
return mg(x,y);
}
if (--tar == 0) {
dir = (dir + 1) % 4;
tar = len;
len += dir & 1
}
}
})(0,0,0,1,1,{'0.0':1})
// IV.)
input.split('\n').reduce((r,p) => r + +p.split(' ').every((w,i,a) => a.indexOf(w)===i),0)
input.split('\n').reduce((r,p) => r + +p.split(' ').map(w => w.split('').sort().join('')).every((w,i,a) => a.indexOf(w)===i),0)
// V.)
for (var a=input.split('\n').map(parseFloat),i=0,n=0; i<a.length||console.log(n); n++,i+=a[i]++);
for (var a=input.split('\n').map(parseFloat),i=0,n=0; i<a.length||console.log(n); n++,i+=a[i]<3?a[i]++:a[i]--);
// VI.)
((a,mem,count) => {
while (!mem[a.join(',')]){
var idx = a.indexOf(Math.max(...a));
var val = a[idx];
mem[a.join(',')] = 1;
a[idx] = 0;
while(val--) {
a[(++idx) % a.length] += 1;
}
count++;
}
return count;
})(input.split('\t').map(s=>+s), {}, 0)
var a = input.split('\t').map(s=>+s);
var mem = {};
var count = 0;
while (mem[a.join(',')] !== 2){
var idx = a.indexOf(Math.max(...a));
var val = a[idx];
mem[a.join(',')] = mem[a.join(',')] ? 2 : 1;
a[idx] = 0;
while(val--) {
a[(++idx) % a.length] += 1;
}
count++;
}
console.log(count - $PREV_RESULT);
// VII.)
for (var key,tt=input.split('\n').reduce((o,s) => (s = s.replace(/ \(\d+\)/, '').replace(/ ->/, ',').split(', '))&(s.slice(1).map(t => o[t]= s[0]))&(key = s[0])&0||o, {}); tt[key]||console.log(key); key = tt[key]);
// TODO: pt.2
// VIII.)
Math.max(...Object.values(input.split('\n').reduce((o,l) => eval(l.replace(/(\S+) (\S+) (\S+) if (\S+) (\S+) (\S+)/,'if ((o.$4||0)$5$6)o.$1=(o.$1||0)$2($3);').replace(/..c\(/,m => m[0]=='d'?'-(':'+('))&0||o,{})))
((x)=>input.split('\n').reduce((o,l) => (eval(l.replace(/(\S+) (\S+) (\S+) if (\S+) (\S+) (\S+)/,'if ((o.$4||0)$5$6)o.$1=(o.$1||0)$2($3);').replace(/..c\(/,m => m[0]=='d'?'-(':'+('))|(x=Math.max(x,...Object.values(o))),o),{})&&x)(-1/0)
// IX.)
(function () {
var count = 0;
var score = 0;
var garb = false;
var expected = [];
var data = input.split('');
console.log(data.length);
for (var i=0;i<data.length;i++) {
if (data[i] == '!') { i++; }
else if (data[i] == '<') { if (expected[0] !== '>' && !garb) { expected.unshift('>'); } garb = true; }
else if (data[i] == '{' && !garb) { score++; expected.unshift('}'); }
else if (data[i] == expected[0]) { if (expected.shift() === '}' && !garb) { count += score; score--; } else { garb = false; } }
}
console.warn(count);
}())
// =============================================================
// =============================================================
(function () {
var count = 0;
var garb = false;
var data = input.split('');
console.log(data.length);
for (var i=0; i<data.length; i++) {
if (data[i] == '!') {
i++;
}
else if (data[i] == '<') {
if (!garb) { garb = true; }
else { count++; }
}
else if (garb) {
if (data[i] == '>') { garb = false; }
else { count++; }
}
}
console.warn(count);
// X.)
// XI.)
((x,y,z) => Math.ceil((x+y+z)/2))(...Object.values(input.split(',').reduce((o,d) => {
switch(d) {
case 'n': o.y++; o.z--; break;
case 's': o.y--; o.z++; break;
case 'nw': o.x--; o.y++; break;
case 'se': o.x++; o.y--; break;
case 'ne': o.x++; o.z--; break;
case 'sw': o.x--; o.z++; break;
}
return o;
}, {x:0,y:0,z:0})).map(Math.abs))
// =============================================================
// =============================================================
var dist = (x,y,z) => {
[x,y,z] = Object.values(x).map(Math.abs);
return Math.ceil((x+y+z)/2);
}
var max = 0;
input.split(',').reduce((o,d,i,a) => {
switch(d) {
case 'n': o.y++; o.z--; break;
case 's': o.y--; o.z++; break;
case 'nw': o.x--; o.y++; break;
case 'se': o.x++; o.y--; break;
case 'ne': o.x++; o.z--; break;
case 'sw': o.x--; o.z++; break;
}
max = Math.max(dist(o), max);
return o;
}, {x:0,y:0,z:0})
// XII.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment