r/adventofcode • • Dec 03 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 3 Solutions -🎄-

--- Day 3: No Matter How You Slice It ---


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

ATTENTION: minor change request from the mods!

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

Card prompt: Day 3 image coming soon - imgur is being a dick, so I've contacted their support.

Transcript:

I'm ready for today's puzzle because I have the Savvy Programmer's Guide to ___.


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!

42 Upvotes

426 comments sorted by

View all comments

2

u/dpeckett Dec 03 '18 edited Dec 03 '18

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

Find the overlapping area of fabric:

{
  split($3,p,/[,:]/); split($4,s,"x");
  for(x=p[1];x<(p[1]+s[1]);x++)
    for(y=p[2];y<(p[2]+s[2]);y++)
      gd[x","y]++;
} END {
  for(sq in gd)if(gd[sq]>1)a++;
  print a
}

Find the tile with no overlap:

{
  id[$1]++;
  split($3,p,/[,:]/); split($4,s,"x");
  for(x=p[1];x<(p[1]+s[1]);x++)
    for(y=p[2];y<(p[2]+s[2]);y++)
      if(v=gd[x","y]) {
        delete id[v]; delete id[$1];
      } else gd[x","y] = $1;
} END {
  for(v in id) print v
}

Execution time:

real 0m1.464s
user 0m1.427s
sys  0m0.029s

Post challenge observations

  • In challenge one, instead of iterating over the full grid in the end block I'd recommend keeping a running counter in the main loop (sum the full area of a tile to a, then decrement on collisions).
  • In both challenges I would make use of the SUBSEP variable provided by AWK.
  • In both challenges I would generalize the overlap detection in a manner that enables better code reuse.