davearonson

davearonson

Help with "alphametics" exercism exercise

First, hi again everybody, been away a while, but getting back into Elixir. Still haven’t landed actual work in it, but seeing more mention of it spurs me to get back in gear learning it. :slight_smile:

I’m working on the very last side-quest^Wexercise on Exercism’s Elixir track, “Alphametics”. The aim is to solve puzzles in which letters are substituted for digits, like “SEND + MORE == MONEY” (you have to figure out what digit each letter stands for, and leading digits must not be zero, like S or M in this case).

I’ve solved it in two different ways, but the efficiency is horrible (taking multiple minutes for a nine or ten unique-letter puzzle), so I was thinking there might be some FP-ish or Elixir-ish thing I’m missing. (I’m coming from a background of about nine years of mostly OO programming with a little imperative mixed in, preceded by 26 years of vice-versa.)

The first way, to describe it in English is:

  • extract the unique letters
  • extract which ones must not be zero (actually I was doing this wrong and getting only the leading digits of the first addend and the sum, but I don’t think that should impact the performance drastically)
  • starting with the pool of all digits, recursively:
    – use Enum.any? to give each digit a chance in turn at the below
    – check if this is zero and the “head” letter is one that must not be zero, in which case return nil
    – else take it as the digit to translate the “head” letter to
    – if we have any letters left, recurse using the rest of the digits and puzzle-letters
    – else see if the puzzle (which should now be all digits) evaluates to true or false (using Code.eval_string); if false, return nil, else I’ve tried both building the map on the way “down” when recursing and on the way back “up” when finishing up after finding the correct solution

The second one is to use a stream to create all possible digit subsets of the correct length, and check one at a time whether they solve it. This turned out to be about 4x faster on the small ones (like up to four unique letters), and I don’t have reliable timing on the larger ones, but the code is large enough that I’m thinking there must be a much more elegant way, both more efficient and more compact. This version is currently visible at Exercism (I didn’t submit the first one but have it in Git if you want to look).

I do not want to take all the time to build in the human-like smarts to figure out “this digit plus that one equals the other one so this one can’t be whatever” and keep track of all that; it seems like a semi-smart brute force solution should be good enough here. :slight_smile:

Any clues, or at least ideas? Thanks!

First Post!

davearonson

davearonson

Update: I’ve fixed the nonzero-letter extraction in my original approach, and submitted it to Exercism at Exercism – it strikes me as more elegant, so it might be a better starting point, but it’s even slower.

Where Next?

Trending in Questions Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
tj0
I’ve been following the steps here for the upgrade from 1.6 to 1.7 and it has gone relatively smoothly all the way till the phoenix_view ...
New
cgraham
Hi! What is currently the best library/method for parsing text and tabular data out of PDF files in Elixir or Erlang?
New
stefanchrobot
Hi, I need a way to handle data migrations in my application. I found an article by @wojtekmach about manual migrations: Automatic and ma...
New
stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
webofbits
Squid Mesh is an open source workflow automation runtime for Elixir applications. It is aimed at Phoenix and OTP apps that want to defin...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
kip
In 2021 I started a new library called Tempo with the objective of modelling time as a set of intervals - not as instants. In 2022 I gave...
New

We're in Beta

About us Mission Statement