r/dailyprogrammer • u/fvandepitte 0 0 • Jan 09 '18
[2018-01-08] Challenge #346 [Easy] Cryptarithmetic Solver
Description
Cryptarithms are a kind of mathematical puzzle. Each puzzle consists of a basic equation of arithmetic (involving addition, subtraction, division, etc.) with words, where each letter represents a different digit. The goal of the puzzle is to find the correct number substitution for each letter in order to make a valid equation.
This classic example (taken from the wikipedia page) was first published in 1924:
S E N D
+ M O R E
_______________
M O N E Y
The solution to this puzzle is:
O = 0,
M = 1,
Y = 2,
E = 5,
N = 6,
D = 7,
R = 8,
and S = 9.
(i.e. 9567 + 1085 = 10652)
Note: Leading zeroes are not allowed in a valid solution.
Task
You will be given a cryptarithm in string form. Your task is to output the letters and corresponding numbers which make up a valid solution to the puzzle.
For the purposes of this challenge, all equations will consist only of addition.
Leading zeroes (in a multi-digit number) are not allowed in a valid solution.
The input is guaranteed to be a valid cryptarithm.
Example
Input:
"THIS + IS + HIS == CLAIM"
Output:
{"A"=>7, "C"=>1, "H"=>8, "I"=>5, "L"=>0, "M"=>6, "S"=>2, "T"=>9}
Challenge Input
"WHAT + WAS + THY == CAUSE"
"HIS + HORSE + IS == SLAIN"
"HERE + SHE == COMES"
"FOR + LACK + OF == TREAD"
"I + WILL + PAY + THE == THEFT"
Output
{"A"=>0, "C"=>1, "E"=>4, "H"=>2, "S"=>3, "T"=>6, "U"=>7, "W"=>9, "Y"=>5}
{"A"=>1, "E"=>8, "H"=>3, "I"=>5, "L"=>0, "N"=>6, "O"=>9, "R"=>7, "S"=>4}
{"A"=>6, "C"=>7, "D"=>3, "E"=>2, "F"=>5, "K"=>8, "L"=>9, "O"=>4, "R"=>0, "T"=>1}
{"A"=>2, "E"=>4, "F"=>7, "H"=>0, "I"=>8, "L"=>3, "P"=>5, "T"=>1, "W"=>9, "Y"=>6}
Bonus
A bonus solution can solve one of the longest known alphametics in a reasonable amount of time:
"TEN + HERONS + REST + NEAR + NORTH + SEA + SHORE + AS + TAN + TERNS + SOAR + TO + ENTER + THERE + AS + HERONS + NEST + ON + STONES + AT + SHORE + THREE + STARS + ARE + SEEN + TERN + SNORES + ARE + NEAR == SEVVOTH"
"SO + MANY + MORE + MEN + SEEM + TO + SAY + THAT + THEY + MAY + SOON + TRY + TO + STAY + AT + HOME + SO + AS + TO + SEE + OR + HEAR + THE + SAME + ONE + MAN + TRY + TO + MEET + THE + TEAM + ON + THE + MOON + AS + HE + HAS + AT + THE + OTHER + TEN == TESTS"
"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"
Finally
Have a good challenge idea?
Consider submitting it to /r/dailyprogrammer_ideas
90
u/TheSlickMachine Jan 09 '18
Are there any of you guys working in software development that look at some of these labelled [Easy] and have no fucking clue how to even begin working on the solutions?
I've probably looked at 50 of these and tried half of them and figured out a solution for one.
16
u/skeeto -9 8 Jan 09 '18 edited Jan 09 '18
C using a bytecode virtual machine to brute force the problem. It solves the first bonus in 2.5s, the second in a few ms, and the third in 18s.
The virtual machine is a stack machine with 5 opcodes: ret, push, add, mul, and eq. A program consists of a series of operations and a constants vector. Only the push instruction takes an operand (represented in the following byte), which is the constants vector index to push. The ret instruction returns the top value on the stack.
The input is compiled into a VM program, and the solution to be tested is represented in the constants vector. To test a candidate, it runs the program and checks the return value. If the program returns 1, the solution is valid. I left my disassembler in the code, which I used to debug my compiler. It's not needed for solving the problem.
The constants vector holds letter assignments as well as the powers of ten. This significantly simplifies the instruction set since it doesn't need to know as much of the problem space.
The VM is not Turing-complete as there is no branching.
Here's the disassembly for SEND + MORE = MONEY:
16
u/ccsiyu Jan 09 '18 edited Jan 10 '18
This problem is perfect for Prolog:
Output:
Challenge 1:
Output:
Bonus #2:
Output:
Code and usage at https://github.com/siyucc/Prolog/tree/master/cryptarithmetic
update:
I wrote a PrologCryptArithmetic.java to generate prolog scripts for a String input like "I + WILL + PAY + THE == THEFT". It gives correct result of 8 + 9833 + 526 + 104 = 10471
I am also able to use the generated script to solve last bonus question. Code is a bit long so I will not post it here, you can find it from my GitHub link above.
Ouput for the last bonus question, it's solved on my t440p-i5-4300m in about 21 seconds:
7
u/cmeister2 Jan 09 '18
Python 3 (maybe 2?) using the Z3 bindings, as I've wanted to play with those for a while. Yes, it's cheating a bit!
Script output here: https://gist.github.com/cmeister2/6a76d856d2ab3a73d3a0cf692a541771. The last cryptarithm hasn't finished decoding on my PC, so maybe there's something inefficient going on. I'll leave it over lunchtime to see if it spits something out.
6
u/ReasonableCause Jan 10 '18
Extremely late to the party, sorry! Short and sweet solution in Haskell, finds the solution in mere seconds:
5
u/olzd Jan 10 '18 edited Jan 10 '18
Prolog:
Output:
Last bonus output:
Python script to generate the Prolog programs:
3
u/chunes 1 2 Jan 10 '18
Factor I wanted to try something silly so I designed a bogo-algorithm with memoization. It takes anywhere from 1-30 seconds to solve the non-bonus inputs.
3
u/LegendK95 Jan 10 '18 edited Jan 10 '18
Haskell - still fairly new to the language so suggestions are welcome. Compiled, this solved the bonus on my average laptop in (8s, under 1s, 7s) respectively.
Also I found this as an excuse to write my own permutations method. I'm willing to explain how the code works if somebody's interested
4
u/claudiodfc Jan 09 '18
I wish I could understand this but I feel so stupid I don't know how this puzzle work. It doesn't make any sense in my head...
2
u/uncleozzy Jan 09 '18
Python 3, using itertools to generate permutations. Ignores permutations based on the leading-zero rule (this speeds up solving the largest problem by about 88%, from 83s to 9.7s). I'm sure it can be improved quite a bit still (the word-value conversion is really slow and hack-y).
2
u/Specter_Terrasbane Jan 09 '18 edited Jan 09 '18
Python 2
Brute force permutation-based. Max solve time for any of the challenge or bonus:
2 mins 34 seconds1 min 27 sec (second last bonus)Edit: doing a simple "first character" check instead of using
reimproved the performanceOutput
2
u/zatoichi49 Jan 09 '18 edited Jan 10 '18
Method:
Brute force using translate for the mapping. Generate each permutation of the digits 0-9, and use this to create a dictionary with the unique letters in the text. Create a second dictionary of the first letters of each word in the text, setting all values to 0. If there's an intersection of these two dictionaries, then skip the permutation as it will produce one or more leading-zero numbers. Cycle through the remaining permutations, stopping when the sum of the translated words equals the value for the translated final word in the text. Return the dictionary with all key:value pairs.
Python 3: (with Bonus)
Output:
2
u/keto166 Jan 10 '18
Java
2
u/zqvt Jan 10 '18 edited Jan 10 '18
Python 3
runs everything in about 45 secs with pypy
2
u/conceptuality Jan 10 '18
Haskell: Brute force
This is a kind of naive brute force approach. Outside of some utility functions, I generate all possible mappings from chars to digits and filter them by the two rules.
For some problems, it starts returning solutions rather quickly, but this has mostly to do with how early on solutions appear i think.
There are probably a ton of optimisations to be made, for instance not rolling my own map.
Code:
2
u/Gprime5 Jan 10 '18 edited Jan 11 '18
Python 3.5
Brute force iterating through permutations
Solutions:
2
u/popillol Jan 10 '18
Go / Golang Playground Link. Unfortunately it's pretty inefficient as it creates/loops through every permutation of 0-9. So for inputs with less than 10 unique letters, it goes through a lot of unnecessary computations.
2
u/[deleted] Jan 10 '18 edited Jan 10 '18
Clojure
Output:
The performance isn't great compared to a couple of other ones here, but it's not terrible either. Last night I wrote a version in kotlin which was surprisingly 10-20% slower even after some profiling + optimizations.
Edit: Marginally less verbose now
2
u/[deleted] Jan 10 '18
Python 3: Just brute force at work here. Let me know how I might be able to improve this in any way.
2
u/octolanceae Jan 10 '18
Python3.6 with Bonus
Took a brute force approach using permutations. Eliminated permutations which would result in a zero first digit. Longest run was for second bonus. It took 65.5 sec. Last bonus ran in 7.7 sec
Output:
Bonus strings were truncated in the posting process.
2
u/DouglasMeyer Jan 10 '18
JavaScript I originally tried to parse the cryptarithm string with regex; that was fun, but splitting was easier/more reliable. This was an enjoyable puzzle. It was fun trying to figure out how to go from an index to a permutation (
0 => [0,1,2,3]; 1 => [1,0,2,3]; ..., around line 36).And the output is an array of objects, as I think it is technically possible to have more than one answer.
2
u/gabyjunior 1 2 Jan 10 '18 edited Jan 10 '18
Solution in C
The solver performs an exhaustive search (finds all valid solutions) on the list of cryptarithms read on standard input.
It sorts the input letters by ascending column and term (group of words), then computes long addition on the sorted data. Partial solution is checked after each column completed to detect incoherence early.
May handle cryptarithms with more than one word on both sides of an equality, and more than one equality.
Completes search for bonus input in 0.9 secs.
Source code
Output for last bonus cryptarithm
2
u/floxbr Jan 11 '18 edited Jan 11 '18
A little late, but here is a solution in Java that takes about 0.3s to solve the challenge inputs (including bonus). It simply tries all possibilies exluding the ones with leading zeros. I considered checking the constraints early to reduce the number of possibilities to check, but as it already is quite fast, this did not seem worth the effort.
2
u/ExcursionSavvy Jan 11 '18
Python
This is my first submission to this community and I hope to start posting more regularly for practice and good habit.
I would absolutely appreciate any comments and support to improve my coding practice. I'm a Mechanical Engineer that uses coding for hobby projects and robotics, and I'm looking to improve my efficiency and overall skill with Python.
I took a brute force approach. I'm not proud if it, but I didn't know a better way without cheating and looking at other's code.
Program:
Output:
2
u/MBPCentral Jan 12 '18 edited Jan 12 '18
VBA solution from a complete noob. Tips/criticisms welcome!
EDIT: sorry about formatting, can't figure out how to sort it out
Dim S As String Dim E As String Dim N As String Dim D As String Dim M As String Dim O As String Dim R As String Dim Y As String Dim SEND As Long Dim MORE As Long Dim MONEY As Long
Dim ixS As Integer Dim ixE As Integer Dim ixN As Integer Dim ixD As Integer Dim ixM As Integer Dim ixO As Integer Dim ixR As Integer Dim ixY As Integer
Dim ixT As Integer Dim DupCount As Integer
ixY = 0 Do While ixY < 10 Y = ixY
Loop
2
u/primaryobjects Jan 13 '18
Javascript
Gist
Solved using random numbers! lol It’s quite fast too.
Output
1
u/tomekanco Jan 10 '18 edited Jan 10 '18
Python 3.6
0.2, 1.6 and 0.9 seconds for bonuses (no compile or)
Thanks to, and by Nikolay Mirin
1
u/[deleted] Jan 10 '18
Ruby
Simple brute force solution. Running benchmarks now and it seems the second bonus is the most time consuming at around 8 minutes.
1
u/Hypersapien Jan 11 '18 edited Jan 11 '18
C# -- Pregenerating every permutation of the numbers 0-9 with the sequence length being the number of distinct letters in the input, filtering out the ones that have a zero in a disallowed index, then just iterating through them.
WHAT + WAS + THY == CAUSE
{"A"=>0, "C"=>1, "E"=>4, "H"=>2, "S"=>3, "T"=>6, "U"=>7, "W"=>9, "Y"=>5}
9206 + 903 + 625 == 10734
6.678 seconds
HIS + HORSE + IS == SLAIN
{"A"=>1, "E"=>8, "H"=>3, "I"=>5, "L"=>0, "N"=>6, "O"=>9, "R"=>7, "S"=>4}
354 + 39748 + 54 == 40156
7.569 seconds
HERE + SHE == COMES
{"C"=>1, "E"=>4, "H"=>9, "M"=>3, "O"=>0, "R"=>5, "S"=>8}
9454 + 894 == 10348
0.755 seconds
FOR + LACK + OF == TREAD
{"A"=>6, "C"=>7, "D"=>3, "E"=>2, "F"=>5, "K"=>8, "L"=>9, "O"=>4, "R"=>0, "T"=>1}
540 + 9678 + 45 == 10263
15.044 seconds
I + WILL + PAY + THE == THEFT
{"A"=>2, "E"=>4, "F"=>7, "H"=>0, "I"=>8, "L"=>3, "P"=>5, "T"=>1, "W"=>9, "Y"=>6}
8 + 9833 + 526 + 104 == 10471
13.958 seconds
TEN + HERONS + REST + NEAR + NORTH + SEA + SHORE + AS + TAN + TERNS + SOAR + TO + ENTER + THERE + AS + HERONS + NEST + ON + STONES + AT + SHORE + THREE + STARS + ARE + SEEN + TERN + SNORES + ARE + NEAR == SEVVOTH
{"A"=>2, "E"=>5, "H"=>3, "N"=>7, "O"=>4, "R"=>6, "S"=>1, "T"=>9, "V"=>8}
957 + 356471 + 6519 + 7526 + 74693 + 152 + 13465 + 21 + 927 + 95671 + 1426 + 94 + 57956 + 93565 + 21 + 356471 + 7519 + 47 + 194751 + 29 + 13465 + 93655 + 19261 + 265 + 1557 + 9567 + 174651 + 265 + 7526 == 1588493
7.782 seconds
SO + MANY + MORE + MEN + SEEM + TO + SAY + THAT + THEY + MAY + SOON + TRY + TO + STAY + AT + HOME + SO + AS + TO + SEE + OR + HEAR + THE + SAME + ONE + MAN + TRY + TO + MEET + THE + TEAM + ON + THE + MOON + AS + HE + HAS + AT + THE + OTHER + TEN == TESTS
{"A"=>7, "E"=>0, "H"=>5, "M"=>2, "N"=>6, "O"=>1, "R"=>8, "S"=>3, "T"=>9, "Y"=>4}
31 + 2764 + 2180 + 206 + 3002 + 91 + 374 + 9579 + 9504 + 274 + 3116 + 984 + 91 + 3974 + 79 + 5120 + 31 + 73 + 91 + 300 + 18 + 5078 + 950 + 3720 + 160 + 276 + 984 + 91 + 2009 + 950 + 9072 + 16 + 950 + 2116 + 73 + 50 + 573 + 79 + 950 + 19508 + 906 == 90393
23.365 seconds
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
{"A"=>1, "E"=>0, "F"=>5, "H"=>8, "I"=>7, "L"=>2, "O"=>6, "R"=>3, "S"=>4, "T"=>9}
9874 + 1 + 5730 + 980305630 + 563 + 122 + 874963704 + 7 + 9022 + 1 + 9120 + 9819 + 512475704 + 794 + 97920 + 974 + 1 + 270 + 980 + 9120 + 65 + 980 + 2149 + 5730 + 863404 + 2190 + 15903 + 980 + 57349 + 5198034 + 5630400 + 980 + 8633634 + 980 + 2149 + 5300 + 93622 + 903375704 + 980 + 863404 + 65 + 5730 + 980 + 93622 + 30494 + 19 + 980 + 8620 + 65 + 264404 + 79 + 74 + 98030 + 9819 + 480 + 496304 + 36204 + 65 + 20198034 + 15903 + 480 + 419745704 + 803 + 8190 + 655 + 98640 + 50134 + 1 + 91490 + 37404 + 14 + 480 + 80134 + 980 + 20149 + 513 + 86340 + 98640 + 5149 + 863404 + 9819 + 57349 + 8013 + 980 + 93622 + 5200 + 655 + 96 + 980 + 563049 + 980 + 863404 + 9819 + 120394 + 31740 + 980 + 491304 + 65 + 980 + 698034 + 14 + 980 + 93622 + 1441724 + 19 + 980 + 96912 + 48759 + 803 + 90098 + 9013 + 8665 + 655 + 96346 + 14 + 980 + 2149 + 86340 + 56350794 + 794 + 2750 + 980 + 57349 + 5198034 + 8013 + 65 + 980 + 8633634 + 98073 + 50134 + 9819 + 980 + 57304 + 563 + 98073 + 501494 + 133049 + 14 + 980 + 57349 + 5198034 + 30409920 + 980 + 2149 + 65 + 980 + 5730 + 863404 + 980 + 2149 + 93622 + 81314404 + 980 + 563049 + 80139 + 5300 + 19 + 2149 + 65 + 980 + 2149 + 93622 + 122 + 65503 + 98073 + 5730 + 8019 + 96 + 980 + 144749034 + 513 + 655 + 980 + 93622 + 51494 + 794 + 2750 + 4863903 + 14 + 49134 + 3740 + 980 + 863404 + 3049 + 4150 + 15903 + 122 + 48130 + 869 + 5748 + 14 + 98073 + 1557271904 + 917263 + 1 + 36654 + 563 + 98073 + 4150 == 5639304404
13.421 seconds
1
u/Scroph 0 0 Jan 12 '18 edited Jan 12 '18
Python solution with useless micro optimizations. I don't remember when was the last time I solved these in Python :
The bonus was solved in 18 minutes on my Atom N455 :
1
u/pi31415926535897 Jan 14 '18 edited Jan 14 '18
Python
I tried to do it with what I already know at first, but that seems beyond my capacity... so I seek some help via googling. I think I might need to study the global variable. TBH time amount is a pain in the arse but I decided to be satisfied with the fact that I at least make it run.
Any comment would be appreciated!
1
u/OldNewbee Jan 15 '18 edited Jan 15 '18
Python 3.6.4
I've been coding for over 50 years but am fairly new at Python. This is also my very first reddit contribution.
My solution is rather stripped-down, but it's pretty efficient. The three bonus items take roughly 6.3, 35.4, and 3.5 seconds respectively.
Here's the output:
"SEND MORE MONEY": 3.85 sec.
{"D"=>7, "E"=>5, "M"=>1, "N"=>6, "O"=>0, "R"=>8, "S"=>9, "Y"=>2}
"THIS + IS + HIS == CLAIM": 3.93 sec.
{"A"=>7, "C"=>1, "H"=>8, "I"=>5, "L"=>0, "M"=>6, "S"=>2, "T"=>9}
"WHAT + WAS + THY == CAUSE": 0.04 sec.
{"A"=>0, "C"=>1, "E"=>4, "H"=>2, "S"=>3, "T"=>6, "U"=>7, "W"=>9, "Y"=>5}
"HIS + HORSE + IS == SLAIN": 2.22 sec.
{"A"=>1, "E"=>8, "H"=>3, "I"=>5, "L"=>0, "N"=>6, "O"=>9, "R"=>7, "S"=>4}
"HERE + SHE == COMES": 0.22 sec.
{"C"=>1, "E"=>4, "H"=>9, "M"=>3, "O"=>0, "R"=>5, "S"=>8}
"FOR + LACK + OF == TREAD": 7.55 sec.
{"A"=>6, "C"=>7, "D"=>3, "E"=>2, "F"=>5, "K"=>8, "L"=>9, "O"=>4, "R"=>0, "T"=>1}
"I + WILL + PAY + THE == THEFT": 3.16 sec.
{"A"=>2, "E"=>4, "F"=>7, "H"=>0, "I"=>8, "L"=>3, "P"=>5, "T"=>1, "W"=>9, "Y"=>6}
"TEN + HERONS + REST + NEAR + NORTH + SEA + SHORE + AS + TAN + TERNS + SOAR + TO + ENTER + THERE + AS + HERONS + NEST + ON + STONES + AT + SHORE + THREE + STARS + ARE + SEEN + TERN + SNORES + ARE + NEAR == SEVVOTH": 6.30 sec.
{"A"=>2, "E"=>5, "H"=>3, "N"=>7, "O"=>4, "R"=>6, "S"=>1, "T"=>9, "V"=>8}
"SO + MANY + MORE + MEN + SEEM + TO + SAY + THAT + THEY + MAY + SOON + TRY + TO + STAY + AT + HOME + SO + AS + TO + SEE + OR + HEAR + THE + SAME + ONE + MAN + TRY + TO + MEET + THE + TEAM + ON + THE + MOON + AS + HE + HAS + AT + THE + OTHER + TEN == TESTS": 35.41 sec.
{"A"=>7, "E"=>0, "H"=>5, "M"=>2, "N"=>6, "O"=>1, "R"=>8, "S"=>3, "T"=>9, "Y"=>4}
"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": 3.54 sec.
{"A"=>1, "E"=>0, "F"=>5, "H"=>8, "I"=>7, "L"=>2, "O"=>6, "R"=>3, "S"=>4, "T"=>9}
Comments welcome.
1
u/gabyjunior 1 2 Jan 17 '18 edited Jan 17 '18
As I adapted my solver to manage other bases than decimal, here are some famous quotes that happen to work as cryptarithms in base 16 (with word "IS" replaced by "=", each have many solutions, it seems very difficult to find one that both means something and has a unique solution):
HEALTHY + CITIZENS = THE + GREATEST + ASSET + ANY + COUNTRY + CAN + HAVE
THE + ESSENCE + OF + ALL + BEAUTIFUL + ART = GRATITUDE
UGLINESS = IN + A + WAY + SUPERIOR + TO + BEAUTY + BECAUSE + IT + LASTS
ALL + WE + HAVE + TO + DECIDE = WHAT + TO + DO + WITH + THE + TIME + THAT + IS + GIVEN + US
1
u/zookeeper_zeke Jan 17 '18 edited Jan 18 '18
I used a C++ constraint solver to make short work of this. I stop after finding the first solution rather than continuing to find the best.
Gecode
I'm using a couple of simple constraints and a depth first search engine that picks the variable with the smallest domain. It solves all of the problems extremely quickly.
1
u/do_hickey Jan 25 '18
Python 3.6
It may not be super speedy, but I'm proud that i figured out how to use 2 generators - 1 for permutations of 0-9 (itertools.permutatinons) and another for combinations of letters and numbers (zip) - and properly got them working the way I wanted! So, slow brute-force it is! Comments welcome.
Source
Output:
1
u/danbcooper Jan 26 '18 edited Jan 26 '18
Really late to the party, but this is my solution in python 3:
It solved the longest one in 2.6 seconds.
1
u/2kofawsome Jul 02 '18 edited Jul 02 '18
python3.6
Used for all questions, although bonuses took a long time (ran code and came back 10min later)
Bonus Outputs: