Advent of Code - Year 2015, Day 10
Problem statement: http://adventofcode.com/2015/day/10
This problem statement is a common word/number play game. The concept is fairly simple, but it can be interesting watching someone work it out if they have never heard the solution before. Thankfully we don't have to solve the problem today, we just need to execute the statements.
Unfortunately, there is no fancy trickery involved in solving this problem. The only real way to solve it is to execute each step using the instructions provided. Fortunately, the instructions are fairly easy.
The biggest trouble with this problem on the practical side is that
strings are a terrible way to store data for processing, especially if you are trying to modify or extend a string that is multiple thousands of characters long. So instead, we use
char list, which are dynamically expandable and easier to manage the data from iteration to iteration. In the end, we don't need to print the final string (though we could), we simply need to know the count of how many characters are in the final string.
The code is fairly simple. We have used
Seq.fold before; here we are using it to keep track for each character in the list whether it is the same as the previous one, in which case we increment the count, or if it is different, in which case we record the new character and restart the count to 1.
The code here is similarly straight forward. You may notice that we are basically doing the work that
Seq.fold does, except we're doing it manually. Again, we simply keep track of the current character and change either the count or the current character depending on whether the character has changed from position to position.