Skip to content

Latest commit

 

History

History
130 lines (91 loc) · 3.33 KB

File metadata and controls

130 lines (91 loc) · 3.33 KB

Cryptarithmetic Puzzle Solver

Description

This repository contains a simple Python program to solve cryptarithmetic puzzles. Cryptarithmetic puzzles involve mathematical equations where letters replace digits. The task is to find the unique digit for each letter such that the equation holds true.

Features

  • Supports custom cryptarithmetic puzzles.
  • Evaluates all possible digit combinations.
  • Skips invalid mappings (e.g., leading zeros).
  • Efficiently finds and displays the correct solution if it exists.

Example Puzzle

AB + AB == BCC

Sample Solution:

61 + 61 = 122

How to Use

  1. Clone the repository:

    git clone https://github.com/sibeux/cryptarithmetic-python.git
  2. Navigate to the project directory:

    cd cryptarithmetic-python
  3. Run the program:

    cryptarithm.py
  4. Enter your puzzle when prompted, e.g., AB + AB = BCC.

Requirements

  • Python 3.6 or higher

How It Works

  1. Extracts unique letters from the puzzle.
  2. Generates all possible digit permutations.
  3. Evaluates each permutation to find the correct solution.

Code Example

from itertools import product

input1 = str(input("Enter the first letter: "))
input2 = str(input("Enter the second letter: "))
print("----------------------------- +")
result = str(input("Enter the result: "))

arrayInput1 = list(input1)
arrayInput2 = list(input2)
arrayResult = list(result)

permInput1 = product(
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], repeat = arrayInput1.__len__())
permInput2 = product(
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], repeat = arrayInput2.__len__())

combInput1 = []
combInput2 = []

for i in list(permInput1):
    combInput1.append(i)

for i in list(permInput2):
    combInput2.append(i)

totalPenyelesaian = 0

for i in combInput1:
    for j in combInput2:
        bilA = int("".join(map(str, i)))
        bilB = int("".join(map(str, j)))
        hasil = bilA + bilB

        if len(str(hasil)) == len(arrayResult):
            numbInput1 = list(i)
            numbInput2 = list(j)
            numbResult = list(str(hasil))

            numbInput1 = list(map(str, numbInput1))
            numbInput2 = list(map(str, numbInput2))

            allList = arrayInput1+arrayInput2+arrayResult
            allListNumb = numbInput1+numbInput2+numbResult
            
            setAllList = set(allList)
            setAllListNumb = set(allListNumb)

            if (len(setAllList) == len(setAllListNumb)):
                count = 0
                for indexSetAllList in range (0, len(setAllList)):
                    listSetAllList = list(setAllList)
                    indexValue = [i for i, x in enumerate(
                        allList) if x == listSetAllList[indexSetAllList]]
                    
                    numbByIndexValue = []
                    for i_indexValue in indexValue:
                        numbByIndexValue.append(allListNumb[i_indexValue])
                        
                    setNumbByIndexValue = set(numbByIndexValue)
                    if len(setNumbByIndexValue) == 1:
                        count += 1
                    else:
                        break
                if count == len(setAllList):
                    totalPenyelesaian += 1
                    print(f"{bilA} + {bilB} = {hasil}")

print(f"Total Penyelesaian: {totalPenyelesaian}")