# ErikSchierboom's solution

## to Alphametics in the C# Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

#### Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Write a function to solve alphametics puzzles.

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.

Write a function to solve alphametics puzzles.

## Hints

• To parse the text, you could try to use the Sprache library. You can also find a good tutorial here.
• You can solve this exercise with a brute force algorithm, but this will possibly have a poor runtime performance. Try to find a more sophisticated solution.
• Hint: You could try the column-wise addition algorithm that is usually taught in school.

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### AlphameticsTest.cs

``````// This file was auto-generated based on version 1.2.0 of the canonical data.

using Xunit;
using System;
using System.Collections.Generic;

public class AlphameticsTest
{
[Fact]
public void Puzzle_with_three_letters()
{
var actual = Alphametics.Solve("I + BB == ILL");
var expected = new Dictionary<char, int>
{
['I'] = 1,
['B'] = 9,
['L'] = 0
};
Assert.Equal(expected, actual);
}

[Fact(Skip = "Remove to run test")]
public void Solution_must_have_unique_value_for_each_letter()
{
Assert.Throws<ArgumentException>(() => Alphametics.Solve("A == B"));
}

[Fact(Skip = "Remove to run test")]
{
Assert.Throws<ArgumentException>(() => Alphametics.Solve("ACA + DD == BD"));
}

[Fact(Skip = "Remove to run test")]
public void Puzzle_with_four_letters()
{
var actual = Alphametics.Solve("AS + A == MOM");
var expected = new Dictionary<char, int>
{
['A'] = 9,
['S'] = 2,
['M'] = 1,
['O'] = 0
};
Assert.Equal(expected, actual);
}

[Fact(Skip = "Remove to run test")]
public void Puzzle_with_six_letters()
{
var actual = Alphametics.Solve("NO + NO + TOO == LATE");
var expected = new Dictionary<char, int>
{
['N'] = 7,
['O'] = 4,
['T'] = 9,
['L'] = 1,
['A'] = 0,
['E'] = 2
};
Assert.Equal(expected, actual);
}

[Fact(Skip = "Remove to run test")]
public void Puzzle_with_seven_letters()
{
var actual = Alphametics.Solve("HE + SEES + THE == LIGHT");
var expected = new Dictionary<char, int>
{
['E'] = 4,
['G'] = 2,
['H'] = 5,
['I'] = 0,
['L'] = 1,
['S'] = 9,
['T'] = 7
};
Assert.Equal(expected, actual);
}

[Fact(Skip = "Remove to run test")]
public void Puzzle_with_eight_letters()
{
var actual = Alphametics.Solve("SEND + MORE == MONEY");
var expected = new Dictionary<char, int>
{
['S'] = 9,
['E'] = 5,
['N'] = 6,
['D'] = 7,
['M'] = 1,
['O'] = 0,
['R'] = 8,
['Y'] = 2
};
Assert.Equal(expected, actual);
}

[Fact(Skip = "Remove to run test")]
public void Puzzle_with_ten_letters()
{
var actual = Alphametics.Solve("AND + A + STRONG + OFFENSE + AS + A + GOOD == DEFENSE");
var expected = new Dictionary<char, int>
{
['A'] = 5,
['D'] = 3,
['E'] = 4,
['F'] = 7,
['G'] = 8,
['N'] = 0,
['O'] = 2,
['R'] = 1,
['S'] = 6,
['T'] = 9
};
Assert.Equal(expected, actual);
}

[Fact(Skip = "Remove to run test")]
{
var actual = Alphametics.Solve("THIS + A + FIRE + THEREFORE + FOR + ALL + HISTORIES + I + TELL + A + TALE + THAT + FALSIFIES + ITS + TITLE + TIS + A + LIE + THE + TALE + OF + THE + LAST + FIRE + HORSES + LATE + AFTER + THE + FIRST + FATHERS + FORESEE + THE + HORRORS + THE + LAST + FREE + TROLL + TERRIFIES + THE + HORSES + OF + FIRE + THE + TROLL + RESTS + AT + THE + HOLE + OF + LOSSES + IT + IS + THERE + THAT + SHE + STORES + ROLES + OF + LEATHERS + AFTER + SHE + SATISFIES + HER + HATE + OFF + THOSE + FEARS + A + TASTE + RISES + AS + SHE + HEARS + THE + LEAST + FAR + HORSE + THOSE + FAST + HORSES + THAT + FIRST + HEAR + THE + TROLL + FLEE + OFF + TO + THE + FOREST + THE + HORSES + THAT + ALERTS + RAISE + THE + STARES + OF + THE + OTHERS + AS + THE + TROLL + ASSAILS + AT + THE + TOTAL + SHIFT + HER + TEETH + TEAR + HOOF + OFF + TORSO + AS + THE + LAST + HORSE + FORFEITS + ITS + LIFE + THE + FIRST + FATHERS + HEAR + OF + THE + HORRORS + THEIR + FEARS + THAT + THE + FIRES + FOR + THEIR + FEASTS + ARREST + AS + THE + FIRST + FATHERS + RESETTLE + THE + LAST + OF + THE + FIRE + HORSES + THE + LAST + TROLL + HARASSES + THE + FOREST + HEART + FREE + AT + LAST + OF + THE + LAST + TROLL + ALL + OFFER + THEIR + FIRE + HEAT + TO + THE + ASSISTERS + FAR + OFF + THE + TROLL + FASTS + ITS + LIFE + SHORTER + AS + STARS + RISE + THE + HORSES + REST + SAFE + AFTER + ALL + SHARE + HOT + FISH + AS + THEIR + AFFILIATES + TAILOR + A + ROOFS + FOR + THEIR + SAFE == FORTRESSES");
var expected = new Dictionary<char, int>
{
['A'] = 1,
['E'] = 0,
['F'] = 5,
['H'] = 8,
['I'] = 7,
['L'] = 2,
['O'] = 6,
['R'] = 3,
['S'] = 4,
['T'] = 9
};
Assert.Equal(expected, actual);
}
}``````
``````﻿using System;
using System.Collections.Generic;
using System.Linq;

public static class Alphametics
{
public static IDictionary<char, int> Solve(string equation) => AlphameticsSolver.Solve(Parse(equation));

private static AlphameticsEquation Parse(string equation)
{
var (left, right) = ParseOperands(equation);
return new AlphameticsEquation(left, right);
}

private static (string[] left, string[] right) ParseOperands(string equation)
{
var leftAndRightOperands = equation.Split(" == ");
var left = leftAndRightOperands[0].Split(" + ");
var right = leftAndRightOperands[1].Split(" + ");

return (left, right);
}
}

public class AlphameticsEquation
{
public AlphameticsEquation(string[] left, string[] right)
{
foreach (var leftSideOperand in left)
ProcessOperand(leftSideOperand, 1);

foreach (var rightSideOperand in right)
ProcessOperand(rightSideOperand, -1);
}

public Dictionary<char, long> LettersWithCount { get; } = new Dictionary<char, long>();
public HashSet<char> NonZeroLetters { get; } = new HashSet<char>();

private void ProcessOperand(string operand, long multiplyCountBy)
{
var letterCount = multiplyCountBy;

for (var i = operand.Length - 1; i >= 0; i--)
{
var letter = operand[i];
if (LettersWithCount.TryGetValue(letter, out var existingCount))
LettersWithCount[letter] = existingCount + letterCount;
else
LettersWithCount[letter] = letterCount;

letterCount *= 10;
}

}
}

public static class AlphameticsSolver
{
private static AlphameticsEquation _equation;

public static IDictionary<char, int> Solve(AlphameticsEquation equation)
{
_equation = equation;

var letterCount = LetterCountCombinations(equation).FirstOrDefault(IsSolution) ?? throw new ArgumentException();
return SolutionForLetterCount(letterCount);
}

private static IEnumerable<int[]> LetterCountCombinations(AlphameticsEquation equation)
=> Enumerable.Range(0, 10)
.ToArray()
.Permutations(equation.LettersWithCount.Count);

private static bool IsSolution(int[] letterCountCombination)
{
if (LetterCountHasInvalidNonZeroLetter())
return false;

return _equation.LettersWithCount.Values
.Zip(letterCountCombination, (count, solutionCount) => count * solutionCount).Sum() == 0;

bool LetterCountHasInvalidNonZeroLetter()
{
var zeroLetterIndex = Array.IndexOf(letterCountCombination, 0);
return zeroLetterIndex != -1 && _equation.NonZeroLetters.Contains(_equation.LettersWithCount.Keys.ElementAt(zeroLetterIndex));
}
}

private static Dictionary<char, int> SolutionForLetterCount(IEnumerable<int> letterCount)
=> letterCount
.Zip(_equation.LettersWithCount.Keys, (x, y) => new KeyValuePair<char, int>(y, x))
.ToDictionary(x => x.Key, x => x.Value);
}

public static class EnumerableExtensions
{
public static IEnumerable<T[]> Permutations<T>(this T[] source, int length)
{
if (length == 1)
return source.Select(t => new[] {t});

return source.Permutations(length - 1)
.SelectMany(t => source.Where(o => !t.Contains(o)),
(t1, t2) => t1.Concat(new T[] {t2}).ToArray());
}
}``````