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 https://exercism.io/my/solutions/9bda75fd72ae4b079f24958e0bc1339f (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 https://exercism.io/my/solutions/9bda75fd72ae4b079f24958e0bc1339f – 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

gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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 Postg...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement