Advent of Code, Day 10

This problem involves finding the length of an element of Conway’s “Look-and-Say” sequence, after a number of iterations on a seed.

The basic solution is easy in Angort:

The function getrun takes a string, and returns a tuple of the digit found, how many repetitions, and the remaining string. The function process repeatedly applies this function. We then just run this function N times, printing the result each time.

This is extremely slow, because Angort’s strings are immutable – there’s an awful amount of copying going on. Angort is not the best language for this kind of problem! However, we can simply write a C++ function to replace process using a large, static buffer. We add this to the library:

and replace our solution with the one-liner

Even the second part, 50 iterations, runs in less than half a second in C++! Immutable strings are a pretty poor solution to many problems, even through they’re nice and safe.

Copyright © Found
Jim Finnis' personal blog

Built on Notes Blog Core
Powered by WordPress