# 出典: mo-notebooks/03/cryptarithms.ipynb

# %%
# install dependencies and select solver

SOLVER = "cbc"

from amplpy import AMPL, ampl_notebook

ampl = ampl_notebook(
    modules=["cbc"],  # modules to install
    license_uuid="default",  # license to use
)  # instantiate AMPL object and register magics

# %%
m = AMPL()
m.read("cryptarithms.mod")

LETTERS = ["S", "E", "N", "D", "M", "O", "R", "Y"]
PAIRS = [(a, b) for a in LETTERS for b in LETTERS if a < b]

m.set["LETTERS"] = LETTERS
m.set["PAIRS"] = PAIRS

m.solve(solver=SOLVER)
assert m.solve_result == "solved", m.solve_result

n = m.var["n"].to_dict()


def letters2num(s):
    return " ".join(map(lambda s: f"{int(round(n[s], 0))}", list(s)))


print("    ", letters2num("SEND"))
print("  + ", letters2num("MORE"))
print("  ----------")
print("= ", letters2num("MONEY"))
