Advent of Code F# — Day 4

ps. look out for all my oth­er solu­tions for Advent of Code chal­lenges here.


Day 4

See details of the chal­lenge here.

The input for today’s chal­lenge looks some­thing like this:


So our first task is to turn these into a more usable for­mat, eg. a tuple of (encrypt­ed name, sec­tor ID, check­sum).

To find the real room names, we need to cal­cu­late the check­sum val­ue from the encrypt­ed name.

To pro­duce the desired sort­ing in LINQ, I’d have used Order­By­De­scend­ing and Then­By. There is no Then­By in any of the F# mod­ules, but F# tuples are ordered the way you expect — by first item, then sec­ond item, and so on.

So, I can achieve the same result as Order­By­De­scend­ing + Then­By with one line instead:

Seq.sortBy (fun (c, cs) -> -(Seq.length cs), c)

pret­ty neat, right?


Part 2

With all the decoy data out of the way, it’s time to decrypt this list and get

The room names are encrypt­ed by a state-of-the-art shift cipher, which is near­ly
unbreak­able with­out the right soft­ware. How­ev­er, the infor­ma­tion kiosk design­ers
at East­er Bun­ny HQ were not expect­ing to deal with a mas­ter cryp­tog­ra­ph­er like

To decrypt a room name, rotate each let­ter for­ward through the alpha­bet a num­ber
of times equal to the room’s sec­tor ID. A becomes B, B becomes C, Z becomes A,
and so on. Dash­es become spaces.

For exam­ple, the real name for qzmt-zixmtkozy-ivhz-343 is very encrypt­ed name.

What is the sec­tor ID of the room where North Pole objects are stored?

First, let’s add a decrypt func­tion:

I found the ques­tion “where North Pole objects are stored” ambi­gious. After a few failed attempts to guess what the room name should be I end­ed up just print­ing them all out and did a quick search for “north”. I’m not sure if the author of the chal­lenge expect­ed you to do that, or maybe I missed a clue some­where..

Any­how, once I fig­ured out what the room name is, get­ting the sec­tor ID was easy.



Liked this post? Why not support me on Patreon and help me get rid of the ads!