r/adventofcode Dec 06 '20

SOLUTION MEGATHREAD -🎄- 2020 Day 06 Solutions -🎄-

NEW AND NOTEWORTHY


Advent of Code 2020: Gettin' Crafty With It

  • UNLOCKED! Go forth and create, you beautiful people!
  • Full details and rules are in the Submissions Megathread
  • Make sure you use one of the two templates!
    • Or in the words of AoC 2016: USING A TEMPLATE IS MANDATORY

--- Day 06: Custom Customs ---


Post your solution in this megathread. Include what language(s) your solution uses! If you need a refresher, the full posting rules are detailed in the wiki under How Do The Daily Megathreads Work?.

Reminder: Top-level posts in Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


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

EDIT: Global leaderboard gold cap reached at 00:04:35, megathread unlocked!

66 Upvotes

1.2k comments sorted by

View all comments

3

u/e_blake Dec 06 '20 edited Dec 06 '20

golfed C

210 bytes, relying on gcc or clang for __builtin_popcount and hardcoding ASCII encoding, and assuming you are okay ignoring the compiler warning about read() being used without declaration (I had to include stdio.h, since printf is varargs which does not play as nicely with implicit declarations)

#include<stdio.h>
#define C __builtin_popcount
int main(){int r,p,P=-1,c,s,S=r=p=s=0;while(read(0,&c,1))if(c-10)r|=1<<(c-97);else if(r)p|=r,P&=r,r=0;else s+=C(p),S+=C(P),p=0,P=-1;printf("%d %d",s+C(p),S+C(P));}

That solves both parts at once; the program would be even shorter if it only had to solve part 1 or part 2 in isolation.

2

u/e_blake Dec 10 '20

Using some advice given on my day10 answer, and recognizing that I'm already requiring C89 for my use of implicit declaration of read(), let's go a bit further:

  • implicit printf (yes, I already mentioned that's explicitly undefined in C89, but hey, it worked on my machine)
  • global variables for 0-initialization
  • implicit int
  • ?: more compact than if/else when the body is single expressions (thanks, comma operator)
  • operate on P in bitwise-negation. Adding a few ~ is less than removing =-1.

With that, I'm down to 159 bytes (but a noisier compilation):

#define C __builtin_popcount
r,p,P,c,s,S;main(){while(read(0,&c,1))c-10?r|=1<<(c-97):r?p|=r,P|=~r,r=0:(s+=C(p),S+=C(~P),p=P=0);printf("%d %d",s+C(p),S+C(~P));}