First puzzle
In this puzzle, we need to convert a string of digits to the string of digits counting how many consecutive numbers appears. Yes, it is confusing if you read it that way, but things are going to get more understandable after seeing some examples:
- “211” => one two, two ones => “1221”
- “1” => one one => “11”
- “21” => one two, one one => “1211”
- “1211” => one one, one two, two one => “111221”
- “111221” => three one, two two, one one => “312211”
Now things are clearer. The solution to the puzzle is the length of the final string after 40 iterations of this process on the puzzle input.
I have to say the code I wrote with Scala was so slow I am ashamed to show it. It gave me the right answer, but… it took 4 hours!!!! To pass to the next puzzle I wrote it in Javascript, and it gave me result in less than 5 seconds:
After feeling frustrated for a while, I decided to find another idea I could use, and I found this Python code here. It is a brilliant solution using RegEx, and here is the Scala translation:
Second puzzle
The second part of the puzzle it to execute the same, but 50 times instead of 40:
You can find this code along with my input and puzzle answers at here.