3 ms·
An alternative using Python's builtin monadic machinery (comprehensions) with a utility function to aid with threading around the remaining alphabet: def t
by joshcorbin 11y ago
An alternative using Python's builtin monadic machinery (comprehensions) with a utility function to aid with threading around the remaining alphabet:
def to_number(*digits):
return sum(d * 10 ** n
for n, d in enumerate(reversed(digits)))
def choose1(digits, *exclude):
for digit in digits.difference(exclude):
yield digit, digits - {digit}
all_digits = set(range(10))
print [
(send, more, money)
for s, e, n, d, send, digits in (
(s, e, n, d, to_number(s, e, n, d), digits)
for s, digits in choose1(all_digits, 0)
for e, digits in choose1(digits)
for n, digits in choose1(digits)
for d, digits in choose1(digits))
for m, o, r, more, digits in (
(m, o, r, to_number(m, o, r, e), digits)
for m, digits in choose1(digits, 0)
for o, digits in choose1(digits)
for r, digits in choose1(digits))
for y, money, digits in (
(y, to_number(m, o, n, e, y), digits)
for y, digits in choose1(digits))
if send + more == money]
I could've actually had the base alphabet called `digits`, but found calling it `all_digits` perhaps less confusing so then `digits` is the one that's only internally visible in the comprehension. This solution is lazy internally, then eagerly pumped to full exhaustion; usually changing the outer `[...]` to `next(...)` is more useful.
I find it very useful to remember that comprehensions deeply connected to monads: https://en.wikipedia.org/wiki/Monad_(functional_programming)#cite_note-4 https://en.wikipedia.org/wiki/Monad_(functional_programming)...
- joshcorbin 11y agoFurther showing the advantage of the pythonic way, is that the above construction remains about as clear when you refactor it for better pruning: ( (to_number(s, e, n, d), to_number(m, o, r, e), to_number(m, o, n, e, y)) for d, digits in choose1(digits) for e, digits in choose1(digits) for y, digits in choose1(digits) if (d + e) % 10 == y for n, digits in choose1(digits) for r, digits in choose1(digits) if (n + r + (d + e) // 10) % 10 == e for o, digits in choose1(digits) if (e + o + (n + r + (d + e) // 10) // 10) % 10 == n for s, digits in choose1(digits, 0) for m, digits in choose1(digits, 0) if (s + m + (e + o + (n + r + (d + e) // 10) // 10) // 10) % 10 == o if (s + m + (e + o + (n + r + (d + e) // 10) // 10) // 10) // 10 == m) Ends up being several order of magnitudes faster since it partially sums each place and prunes aggressively. You can eliminate the sub-expressions, but it has noise impact on the run time (likely due to trading off temporary tuples for cheap arithmetic): ( (to_number(s, e, n, d), to_number(m, o, r, e), to_number(m, o, n, e, y)) for d, digits in choose1(digits) for e, digits in choose1(digits) for y, digits in choose1(digits) if (d + e) % 10 == y for n, digits in choose1(digits) for r, digits in choose1(digits) if (n + r + (d + e) // 10) % 10 == e for o, digits in choose1(digits) if (e + o + (n + r + (d + e) // 10) // 10) % 10 == n for s, digits in choose1(digits, 0) for m, digits in choose1(digits, 0) if (s + m + (e + o + (n + r + (d + e) // 10) // 10) // 10) % 10 == o if (s + m + (e + o + (n + r + (d + e) // 10) // 10) // 10) // 10 == m)