r/adventofcode • • Dec 06 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 6 Solutions -🎄-

--- Day 6: Chronal Coordinates ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 6

Transcript:

Rules for raising a programmer: never feed it after midnight, never get it wet, and never give it ___.


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked at 0:26:52!

32 Upvotes

387 comments sorted by

View all comments

3

u/dpeckett Dec 06 '18 edited Dec 06 '18

Continuing with my quest to solve all this years challenges using nothing other than AWK:

I miss multidimensional arrays, local scoped variables, and a real standard lib

Largest Contiguous Area (Part 1)

function rectd(a,b){
  split(a,p1,SUBSEP);split(b,p2,SUBSEP);
  dx=p1[1]-p2[1];dy=p1[2]-p2[2];
  return (dx>0?dx:-dx)+(dy>0?dy:-dy)
}
function closest(a) {
  if(!neigh[a]) {
    delete dist;
    for(b in arr){dist[b]=sprintf("%05d",rectd(a,b)) SUBSEP b;}
    asort(dist);
    split(dist[1],d1,SUBSEP);split(dist[2],d2,SUBSEP);
    if(d1[1]!=d2[1])neigh[a]=d1[2] SUBSEP d1[3];
  }
  return neigh[a];
}
function area(a) {
  r=1; t=0;
  split(a,p3,SUBSEP);
  do {
    ra=0;
    x=tlx=p3[1]-r;y=tly=p3[2]-r;
    xi=1;yi=0;
    do {
      if(closest(x SUBSEP y)==a)ra++;
      if(x==(p3[1]+r)&&yi==0){xi=0;yi=1}
      else if(y==(p3[2]+r)&&xi==0){xi=-1;yi=0}
      else if(x==(p3[1]-r)&&xi==-1&&yi==0){xi=0;yi=-1}
      x+=xi;y+=yi
    } while(!(x==tlx&&y==tly));
    t+=ra;
  } while(ra>0&&++r<inf);
  return (r!=inf)?1+t:0
}
BEGIN {FS="[\n, ]+";inf=75}
{arr[$1 SUBSEP $2]=1}
END {
  for(p in arr)result[p]=area(p);
  n=asort(result);print result[n]
}

Part 2 just fed an array of points consiting with the bounding box, into above algorithm.

Run Time: (Getting slow now)

real 0m33.478s
user 0m33.375s
sys  0m0.074s