Advent of Code F# — Day 7

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


Sor­ry for the delay in get­ting this post out today, I enjoyed some real­ly fine Ramen with some of the love­ly peo­ple I worked with at Yubl and only man­aged to find time to do the chal­lenge and write this post late in the evening.

(ps. here are some of the best peo­ple I have ever had the plea­sure to work with )


any­ways, let’s get back to the main top­ic…


Day 7

See details of the chal­lenge here.

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


Look­ing at the input, they all fol­low a sim­i­lar pat­tern and we can sep­a­rate them out with String.Split(‘[‘, ’]’) and end up with an array where the odd num­ber ele­ments are the hyper­net sequences.

We still need a way to look for ABBAs, which for­tu­nate­ly can be achieved eas­i­ly with Seq.windowed and a bit of pat­tern match­ing (see the hasAB­BA func­tion below).


Part 2

You would also like to know which IPs sup­port SSL (super-secret lis­ten­ing).

An IP sup­ports SSL if it has an Area-Broad­cast Acces­sor, or ABA, any­where in the
super­net sequences (out­side any square brack­et­ed sec­tions), and a cor­re­spond­ing
Byte Allo­ca­tion Block, or BAB, any­where in the hyper­net sequences. An ABA is any
three-char­ac­ter sequence which con­sists of the same char­ac­ter twice with a
dif­fer­ent char­ac­ter between them, such as xyx or aba. A cor­re­spond­ing BAB is the
same char­ac­ters but in reversed posi­tions: yxy and bab, respec­tive­ly.

For exam­ple:

aba[bab]xyz sup­ports SSL (aba out­side square brack­ets with cor­re­spond­ing bab
with­in square brack­ets).
xyx[xyx]xyx does not sup­port SSL (xyx, but no cor­re­spond­ing yxy).
aaa[kek]eke sup­ports SSL (eke in super­net with cor­re­spond­ing kek in hyper­net;
the aaa sequence is not relat­ed, because the inte­ri­or char­ac­ter must be dif­fer­ent).
zazbz[bzb]cdb sup­ports SSL (zaz has no cor­re­spond­ing aza, but zbz has a
cor­re­spond­ing bzb, even though zaz and zbz over­lap).
How many IPs in your puz­zle input sup­port SSL?

Part 2 is slight­ly more chal­leng­ing as it requires to track the ABAs from the sequences that are not part of the hyper­net sequences, and look for cor­re­spond­ing BABs in the hyper­net sequences (seri­ous­ly, who thought of these acronym…).

We can use the same tech­nique of + pat­tern match­ing to find all the ABAs, but we also need a has­BAB func­tion to check if any of the cor­re­spond­ing BABs exists in a hyper­net sequence.

Whilst I was at it, I also threw in a ToB­A­Bs active pat­tern to con­vert an array of ABAs into their cor­re­spond­ing BABs.

The ques­tion was slight­ly ambigu­ous — I wasn’t sure if all the cor­re­spond­ing BABs need to exist in the hyper­net sequences or just one, I went with the lat­er and that got me the right answer so I guess I got lucky there.