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?

Popular in Questions Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

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
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

We're in Beta

About us Mission Statement