Tracks
/
AWK
AWK
/
Exercises
/
Alphametics
Alphametics

Alphametics

Hard

Instructions

Given an alphametics puzzle, find the correct solution.

Alphametics is a puzzle where letters in words are replaced with numbers.

For example SEND + MORE = MONEY:

  S E N D
  M O R E +
-----------
M O N E Y

Replacing these with valid numbers gives:

  9 5 6 7
  1 0 8 5 +
-----------
1 0 6 5 2

This is correct because every letter is replaced by a different number and the words, translated into numbers, then make a valid sum.

Each letter must represent a different digit, and the leading digit of a multi-digit number must not be zero.

Input

Your script will receive the alphametic puzzle as a single string via standard input (stdin). The puzzle string follows the format WORD1 + WORD2 + ... == RESULT.

For example:

SEND + MORE == MONEY

Output

If the puzzle has a solution, print a single line consisting of space-separated LETTER=DIGIT pairs, sorted alphabetically by letter.

For the example above, the output should be:

D=7 E=5 M=1 N=6 O=0 R=8 S=9 Y=2

If the puzzle has multiple valid solutions, printing any one of them is acceptable.

If the puzzle has no solution, print nothing (an empty line).

Edit via GitHub The link opens in a new window or tab
AWK Exercism

Ready to start Alphametics?

Sign up to Exercism to learn and master AWK with 3 concepts92 exercises, and real human mentoring, all for free.