Today’s Advent of Code challenge was a relatively kind one, and I’m pretty sure if I tried hard enough I could get this down to a single line of code. But a great article from Pierre Irrmann reminded me that with F# it can be all too easy to go overboard on pipelines and composition and end up with clever code that is incomprehensible.

So I decided to take an “outside” in approach. I needed a solve function that took a sequence defining the “disks” and would return an integer by finding the first time at which all the disks were aligned. The disk definitions were tuples of number of positions and starting position (if I’d been following all of Pierre’s advice these would have been types instead). And the isSolution function I stubbed out to return true. Obviously this meant my test case would return 0 initially.

So next was to implement isSolution. If start time is 5, then the first disk must be open at 6, the second disk must be open at 7 and so on. So I used Seq.indexed to get a tuple of the disk definition and an incrementing number, and then tested whether for each disk and time offset the slot was open. Again isOpenAtT had not been implemented yet so was hard-coded to return true.

As I said, there’s no doubt the solution could be made much more succinct, but the outside-in approach of starting with the solve function worked very well and is one I will have to try again in future. Full code is available on GitHub as usual.

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: