Avatar of w1zeman1p

w1zeman1p's solution

to Alphametics in the Ruby Track

Published at Feb 15 2019 · 0 comments
Instructions
Test suite
Solution

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.


For installation and learning resources, refer to the Ruby resources page.

For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:

gem install minitest

If you would like color output, you can require 'minitest/pride' in the test file, or note the alternative instruction, below, for running the test file.

Run the tests from the exercise directory using the following command:

ruby alphametics_test.rb

To include color from the command line:

ruby -r minitest/pride alphametics_test.rb

Submitting Incomplete Solutions

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

alphametics_test.rb

require 'minitest/autorun'
require_relative 'alphametics'

# Common test data version: 1.3.0 361cf3c
class AlphameticsTest < Minitest::Test
  def test_puzzle_with_three_letters
    # skip
    puzzle = 'I + BB == ILL'
    expected = { 'B' => 9, 'I' => 1, 'L' => 0 }
    assert_equal expected, Alphametics.solve(puzzle)
  end

  def test_solution_must_have_unique_value_for_each_letter
    skip
    puzzle = 'A == B'
    expected = {}
    assert_equal expected, Alphametics.solve(puzzle)
  end

  def test_leading_zero_solution_is_invalid
    skip
    puzzle = 'ACA + DD == BD'
    expected = {}
    assert_equal expected, Alphametics.solve(puzzle)
  end

  def test_puzzle_with_two_digits_final_carry
    skip
    puzzle = 'A + A + A + A + A + A + A + A + A + A + A + B == BCC'
    expected = { 'A' => 9, 'B' => 1, 'C' => 0 }
    assert_equal expected, Alphametics.solve(puzzle)
  end

  def test_puzzle_with_four_letters
    skip
    puzzle = 'AS + A == MOM'
    expected = { 'A' => 9, 'M' => 1, 'O' => 0, 'S' => 2 }
    assert_equal expected, Alphametics.solve(puzzle)
  end

  def test_puzzle_with_six_letters
    skip
    puzzle = 'NO + NO + TOO == LATE'
    expected = { 'A' => 0, 'E' => 2, 'L' => 1, 'N' => 7,
                 'O' => 4, 'T' => 9 }
    assert_equal expected, Alphametics.solve(puzzle)
  end

  def test_puzzle_with_seven_letters
    skip
    puzzle = 'HE + SEES + THE == LIGHT'
    expected = { 'E' => 4, 'G' => 2, 'H' => 5, 'I' => 0,
                 'L' => 1, 'S' => 9, 'T' => 7 }
    assert_equal expected, Alphametics.solve(puzzle)
  end

  # The obvious algorithm can take a long time to solve this puzzle,
  # but an optimised solution can solve it fairly quickly.
  # (It's OK to submit your solution without getting this test to pass.)
  def test_puzzle_with_eight_letters
    skip
    puzzle = 'SEND + MORE == MONEY'
    expected = { 'D' => 7, 'E' => 5, 'M' => 1, 'N' => 6,
                 'O' => 0, 'R' => 8, 'S' => 9, 'Y' => 2 }
    assert_equal expected, Alphametics.solve(puzzle)
  end

  # The obvious algorithm can take a long time to solve this puzzle,
  # but an optimised solution can solve it fairly quickly.
  # (It's OK to submit your solution without getting this test to pass.)
  def test_puzzle_with_ten_letters
    skip
    puzzle = 'AND + A + STRONG + OFFENSE + AS + A + GOOD == DEFENSE'
    expected = { 'A' => 5, 'D' => 3, 'E' => 4, 'F' => 7,
                 'G' => 8, 'N' => 0, 'O' => 2, 'R' => 1,
                 'S' => 6, 'T' => 9 }
    assert_equal expected, Alphametics.solve(puzzle)
  end

  # The obvious algorithm can take a long time to solve this puzzle,
  # but an optimised solution can solve it fairly quickly.
  # (It's OK to submit your solution without getting this test to pass.)
  #
  # Remove 'do_not_' from the test name to run this test.
  def do_not_test_puzzle_with_ten_letters_and_199_addends
    skip
    puzzle = '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'
    expected = { 'A' => 1, 'E' => 0, 'F' => 5, 'H' => 8,
                 'I' => 7, 'L' => 2, 'O' => 6, 'R' => 3,
                 'S' => 4, 'T' => 9 }
    assert_equal expected, Alphametics.solve(puzzle)
  end
end
class Alphametics
  attr_reader :addends
  attr_reader :sum

  def self.solve(puzzle)
    puzzle = puzzle.gsub(/ /, "")
    *addends, sum = puzzle.split(/\+|==/)
    Alphametics.new(addends, sum).solve
  end

  def initialize(addends, sum)
    @addends = addends
    @sum = sum
  end

  def unique_letters
    @unique_letters ||= "#{addends.join("")}#{sum}".chars.uniq.sort
  end

  def letters
    @letters ||= unique_letters.join("")
  end

  def solve
    permutations = (0..9).to_a.permutation(unique_letters.length)
    permutations.each do |values|
      addends, sum = numberized(values)
      if equal?(addends, sum) && valid?(addends, sum)
        return unique_letters.zip(values).to_h
      end
    end
    {}
  end

  def numberized(values)
    values = values.map(&:to_s).join("")
    num_sum = sum.tr(letters, values)
    num_addends = addends.map do |a|
      a.tr(letters, values)
    end
    [num_addends, num_sum]
  end

  def valid?(addends, sum)
    addends.none? {|a| a.start_with?("0")} && !sum.start_with?("0")
  end

  def equal?(addends, sum)
    sum.to_i == addends.map(&:to_i).inject(0, :+)
  end
end

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?