Today’s Advent of Code challenge required us to count how many pairs of three coordinates represented possible triangles. The calculation itself is relatively straightforward – we just sort the sides and then see if the shortest two add up to greater than the longest.

I used regex again to pick the numbers out of the input file. One gotcha of the F# compiler’s type inference is that my code compiled and ran fine with the triangle sides as strings instead of integers since isPossibleTriangle works just fine with strings.

For the second part, it turns out that the sides of each triangle are not all on one line of the input file, but spread across three lines. This meant I wanted a batch function like MoreLINQ has, to batch the input into three lines. And I needed a rotate function that would take input like [a;b][c;d] and turn it into [a;c][b;d]. I also found myself wanting a simple helper to give numbers in the range 0 to n - 1.

I wouldn’t be surprised if F# already has some more elegant ways to solve these, so do let me know in the comments, but implementing them myself did at least give me the chance to use the array comprehension and slicing syntaxes:

With these transformations in place, getting the sides is a case of batching, rotating, and then expanding out again with Seq.collect, which gets us back to a sequence of arrays of triangle side lengths which we can count in the same way as before:

Of course, once again this solution ends up a bit more verbose than the equivalent procedural code would have been, but if you take away the very generic helpers I had to make, the core solution is fairly simple. Full code available here.

As always, do check out the reddit thread for ingenious (and insane) solutions in other languages. Like last year, it does seem that Python is particularly well suited to these types of problems, with some of the most elegant and concise solutions for all three days so far being in Python.

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: