USSR used a fixed stencil to write postal codes on mail envelopes. It’s similar to a seven-segment display, but had 9 segments with a standard way to write all 10 digits:
I stumbled upon an article (in Russian) claiming, and confirming, that only 4 of the 9 segments are enough to distinguish all 10 digits. The article had a small challenge to write a program to find out the number of different ways to pick 4 segments that allow to distinguish all the digits. It’s a small, yet interesting problem to solve; my program is in Haskell and is available at https://github.com/eunikolsky/postalcode.
I’ve had a few versions of the script, but didn’t save them as separate commits. It took me about an hour to arrive at the final version:
First, I hardcoded the digits by manually entering the empty/fill state of every segment (this code is commented on lines 33–46). But it wasn’t obvious at all what the lists meant and how they were constructed and whether I entered them correctly.
This comment (also a script in Haskell) gave me an idea of parsing the segments out of the visual representation. I wrote down each digit as its own 2D list and wrote a parser to read the segments. That worked, but the source file got quite a bit longer because each digit used 7 lines. Each digit had only 9 segments like this:
123456
[" - "," |"," "," / "," - "]
Then I compacted all the digits into one list of strings like in that comment above. Thus, I needed to cut out each digit out of every string, but it was easy by transposing the list (so that the strings had the digits rotated 90° clockwise), taking 3 lines for each digit and transposing each of those lists back (rotating 90° counter-clockwise).
Finally, that 2 above doesn’t look pretty because there is a lot of empty space. I updated my parser to expect and ignore certain characters on joints, allowing me to parse this:
It was easy to write the script in Haskell because the type system helped me on the way and I liked that the text parser Text.Parsec is available out of the box.