Show HN: Gorunwasm – my weekend project to aid developingGo WASM programs
github.com2 ポイント投稿者 joshcorbin0 コメント
(
(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) 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.
Yes... imagine...
Tell me you've got little empathy for the autist mind without telling me...