Today’s Advent of Code challenge was relatively kind, although time did not permit me to do much refactoring of my solution. I’ve been using all the test cases in the problem descriptions this year which has done wonders for the accuracy of my final answers (both right first time again today), but it does tend to shape the way you tackle to problem so that you can write tests for intermediate pieces.

In this challenge, we had to generate the next string in a sequence based on the previous string. This afforded another opportunity for the ever handy Seq.windowed function which was ideal for giving us the relevant three characters from the line above to calculate the character on the next line:

Since we can generate the next line from the previous one, I wanted a function to emit a sequence of lines given a starting line. I tried a bunch of different ideas including recursive sequences, Seq.unfold, but it turned out that Seq.scan gave me the simplest route to what I wanted:

Now we have all the bits in place to solve parts a and b of the puzzle. Thankfully there was no need to aggressively optimise for part b since it solved it in about 20 seconds, but there is of course plenty of room for improvement.

About Mark Heath

I'm a Microsoft MVP and software developer based in Southampton, England, currently working as a Software Architect for NICE Systems. I create courses for Pluralsight and am the author of several open source libraries. I currently specialize in architecting Azure based systems and audio programming. You can find me on: