Advent of Code F# – Day 6

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

 

Day 6

See details of the chal­lenge here.

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

cmezkqgn
nmzrgcft
ydp­nd­cps
zji­h­hows
kvp­txs­rx
ubb­vug­wq

Since the only dif­fer­ence between the 2 parts of this chal­lenge is how the char­ac­ters in a col­umn are sort­ed by count, so we can cap­ture the com­mon flow to solv­ing both parts in a solve func­tion below.

Let’s walk through what’s hap­pen­ing here step by step.

  1. first we col­lect all the char­ac­ters of each col­umn into a seq<char[]>
  2. then for each char[] we
    1. group by the chars by itself
    2. map the resul­ing seq<char * seq<char» into a sequence of char and count, ie seq<char * int>
    3. use the sup­plied sort­By func­tion to sort this sequence by count
    4. take the first (most common/least com­mon depend­ing on the sort­By func­tion)
    5. return the char
  3. stich the chars togeth­er into a good old fash­ioned string

 

So now, solv­ing part 1 is a sim­ple one-lin­er:

solve Seq.sortByDescending

 

Part 2

Of course, that would be the mes­sage — if you hadn’t agreed to use a mod­i­fied
rep­e­ti­tion code instead.

In this mod­i­fied code, the sender instead trans­mits what looks like ran­dom data,
but for each char­ac­ter, the char­ac­ter they actu­al­ly want to send is slight­ly
less like­ly than the oth­ers. Even after sig­nal-jam­ming noise, you can look at
the let­ter dis­tri­b­u­tions in each col­umn and choose the least com­mon let­ter to
recon­struct the orig­i­nal mes­sage.

In the above exam­ple, the least com­mon char­ac­ter in the first col­umn is a; in
the sec­ond, d, and so on. Repeat­ing this process for the remain­ing char­ac­ters
pro­duces the orig­i­nal mes­sage, advent.

Giv­en the record­ing in your puz­zle input and this new decod­ing method­ol­o­gy, what
is the orig­i­nal mes­sage that San­ta is try­ing to send?

Because of the work we had done ear­li­er, solv­ing part 2 is also a one-lin­er:

solve Seq.sortBy

 

Links