Skip to content

Instantly share code, notes, and snippets.

@emplv
emplv / AoC_2019_d10.js
Created December 11, 2019 12:41
AoC 2019 - day 10
d=(I=``.trim().split`\n`)[0].length;
// part 1
Math.max(I.flatMap(x=>x.split``).map((x,i,a)=>x=='#'?a.map((y,j)=>i!=j&&x==y?Math.atan2(j%d-i%d,(j/d|0)-(i/d|0)):NaN).filter((z,k,b)=>1/z&&b.findIndex(q=>q==z)==k).length:0))
// part 2
//
@emplv
emplv / AoC_2019_d7.js
Last active December 9, 2019 18:39
AoC 2019 - day 7
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
@emplv
emplv / AoC_2019_d8.js
Last active December 8, 2019 21:31
Advent of Code - day 8
const INPUT = ``.trim();
// part 1
INPUT.match(/.{150}/g).map(X=>[X.replace(/1|2/g,x=>(x<2?O++:T++,''),O=T=0),O*T]).sort()[0][1]
// part 2
// INPUT.match(/.{150}/g).reduce((t,b)=>[...t].map((x,i)=>x<2?x:b[i]).join``).match(/.{25}/g)
// [...INPUT].map((x,i,a)=>a[j=i%150]<2?a[j]:a[j]=x).join``.slice(-150).match(/.{25}/g)
[...INPUT].reduce((r,x,i)=>(r[j=i%150]=r[j]<2?r[j]:x,r),[]).join``.match(/.{25}/g)
//
@emplv
emplv / AoC_2019_d6.js
Created December 6, 2019 07:39
AoC 2019 - day 6
const INPUT = `
COPY & PASTE HERE
`.trim``.split`\n`;
// prepare orbit map
INPUT.map(x=>o[[s,y]=x.split`)`,y]=s,o={}); // o == all orbits
// Part 1
c={}; // cache
a=k=>c[k]||(o[k]?c[k]=1+a(o[k]):0); // get all orbiters count
t=Object.keys(o).reduce((t,c)=>t+a(c),0); // count total
// Part 2
@emplv
emplv / AoC_2019_d5.js
Last active December 6, 2019 07:39
AoC 2019 - day 5
const INPUT = `
COPY & PASTE HERE
`.trim``.split`,`;
// part 1
(z=y=>([q,w,e,r]=t.slice(y),99-(u=q%100))?(t[[,r,r,w][u]]=[,(i=(b=q/100|0)&1?w:t[w])-+-(o=b&8?e:t[e]),i*o,x][u],z(y+2**2**(u<3))):+i)(0,x=1,t=[...INPUT]);
// part 2 (also works for part1)
(z=y=>([q,w,e,r]=t.slice(y),99-(u=q%100))?(t[[,r,r,w][u%6]]=[,(i=(b=q/100|0)&1?w:t[w])-+-(o=b&8?e:t[e]),i*o,x,,,,i<o,i==o][u],z(y+=u%7<3?4:u<5?2:u<6^!+i?+o-y:3)):i)(0,x=5,t=[...INPUT]);
//
x=5,t=[...fetchSync('http://ix.io/23CO').trim().split(',')];
//
@emplv
emplv / AoC_2019_d2.js
Last active December 4, 2019 11:01
AoC 2019 - day 2
const INPUT = `
COPY & PASTE HERE
`.trim().split`,`;
function doInstructions([...arr],one=12,two=2) {
for(arr[1]=one,arr[2]=two,i=0;[o,a,b,t]=arr.slice(i),99-o||~'12'.search(o);i+=4)arr[t]=2==o?arr[a]*arr[b]:arr[a]- -arr[b];
return arr[0];
}
// part 1
@emplv
emplv / AoC_2019_d1.js
Last active December 5, 2019 07:50
AoC 2019 - day 1
const INPUT = `
COPY & PASTE HERE
`.trim().split`\n`;
// part 1
INPUT.map(x=>t+=(i=x/3-2|0)>0?i:0,t=0)|t;
// part 2
INPUT.map(f=i=>(i=i/3-2|0)>0?f(i,t+=i):0,t=0)|t
@emplv
emplv / AoC_2019_d4.js
Created December 4, 2019 08:21
AoC 2019 - day 4
const INPUT = '000000-000000';
const [ from, to ] = INPUT.split`-`;
// Part 1
for(x=0,i=from;i<to;)
(a=[...''+i++]).every((c,j)=>!j||+c>=+a[j-1])
&&a.some((c,j)=>c==a[j-1])
&&x++,x
// Part 2
for(x=0,i=236491;i<713786;)
(a=[...''+i++]).every((c,j)=>!j||+c>=+a[j-1])
@emplv
emplv / AoC_2019_d3.js
Last active December 3, 2019 10:51
AoC 2019 - day 3
const INPUT = `
COPY & PASTE HERE INPUT LINES
`;
const [firstLine,secondLine]=INPUT.trim().split`\n`;
const first = firstLine.split`,`;
const second = secondLine.split`,`;
const wires = {};
[first,second].forEach(input => {
const currentWire = {};
for(c=Array(32).fill(2);l=readline(p='');c[e]={S:3,A:c[e]&c[g],O:c[e]|c[g]}[d]|0)[[d],e,g]=l.split` `
c.map(f=>p='0.?1'[f]+p)
writeline(p)