Brainteaser 1647: Happy Easter
From The Sunday Times, 3rd April 1994 [link]
Here is a sum with an odd total in which digits have been consistently replaced by letters, different letters being used for different digits:
Find the value of EGGS.
[teaser1647]

Jim Randell 10:27 am on 21 March 2024 Permalink |
Using the [[
SubstitutedExpression]] solver from the enigma.py library.The following run file executes in 76ms. (Internal runtime of the of the generated program is 85µs).
Run: [ @replit ]
Solution: EGGS = 4882.
The sum is: 1403 + 340143 + 60997 = 402543.
Which assigns 9 of the ten digits, leaving G = 8.
LikeLike
GeoffR 7:03 pm on 21 March 2024 Permalink |
from itertools import permutations digits = set('0123456789') for R in (1,3,5,7,9): r = str(R) q1 = digits - {r} for p1 in permutations(q1, 3): d, e, a = p1 dear = int(d + e + a + r) reader = int(r + e + a + d + e + r) q2 = q1 - set(p1) for p2 in permutations(q2, 3): h, p, y = p2 happy = int(h + a+ p + p + y) EASTER = dear + reader + happy easter = str(EASTER) if easter[0] == e and easter[4] == e: if easter[1] == a and easter[-1] == r: s, t = easter[2], easter[3] used = set((e, a, s, t, r, d, h, p, y)) if len(used) == 9: g = digits - used G = int(g.pop()) E, S = int(e), int(s) EGGS = 1000*E + 110*G + S print(f"EGGS = {EGGS}") print(f"Sum: {dear} + {reader} + {happy} = {EASTER}") # EGGS = 4882 # Sum: 1403 + 340143 + 60997 = 402543LikeLike
Frits 11:11 am on 22 March 2024 Permalink |
from functools import reduce # convert digits sequence to number d2n = lambda s: reduce(lambda x, y: 10 * x + y, s) dgts = set(range(10)) # R is odd as total is odd but not 5 (Y becomes 5) or 9 (E becomes 10) for R in [1, 3, 7]: if len(q1 := dgts - {R, E := R + 1, Y := 10 - R}) != 7: continue for A in q1: # carry + E + H = 10 + A --> A < carry + E if not (A < 2 + E): continue if len(q2 := q1 - {A, P := 9 - A}) != 5: continue APPY = d2n([A, P, P, Y]) for D in q2: if D == 0: continue DEAR = d2n([D, E, A, R]) READER = d2n([R, E, A, D, E, R]) # sum last 4 columns carry, tot = divmod(DEAR + READER % 10000 + APPY, 10000) # carry + E + H = 10 + A H = 10 + A - carry - E if H in {0, D} or H not in q2: continue # tot must be equal to STER S, T = divmod(tot // 100, 10) if len(q3 := q2 - {D, H, S, T}) != 1: continue HAPPY = 10000 * H + APPY EASTER = d2n([E, A, S, T, E, R]) if DEAR + READER + HAPPY != EASTER: continue print(f"answer: EGGS = {d2n([E, (G := q3.pop()), G, S])}") ''' print() print(f"{DEAR:>6}") print(f"{READER:>6}") print(f"{HAPPY:>6}") print("------") print(f"{EASTER:>6}") '''LikeLike
Ruud 6:42 am on 21 April 2024 Permalink |
Brute force, extremely simple, solution. Runs within 30 seconds …
import itertools from istr import istr for d, e, a, r, h, p, y, s, t,g in istr(itertools.permutations("0123456789", 10)): if r.is_odd() and (d | e | a | r) + (r | e | a | d | e | r) + (h | a | p | p | y) == (e | a | s | t | e | r): print('eggs = ', e|g|g|s)LikeLike
Frits 10:55 am on 22 April 2024 Permalink |
Hi Ruud, with CPython your version runs for 140seconds on my PC. A similar program without the istr package runs for 3 seconds with CPython (PyPy is faster).
from itertools import permutations for d, e, a, r, h, p, y, s, t, g in permutations("0123456789"): if (r in "13579" and "0" not in (d + r + h + e) and int(d+e+a+r) + int(r+e+a+d+e+r) + int(h+a+p+p+y) == int(e+a+s+t+e+r)): print('eggs = ', e+g+g+s)LikeLike
GeoffR 11:10 am on 21 April 2024 Permalink |
Using primary school addition method of columns and carry digits.
% A Solution in MiniZinc include "globals.mzn"; var 1..9:D; var 1..9:E; var 0..9:A; var 1..9:R; var 0..9:S; var 0..9:T; var 0..9:G; var 1..9:H; var 0..9:P; var 0..9:Y; var 1000..9999:EGGS = 1000*E + 110*G + S; constraint R in {1,3,5,7,9}; % Column carry digits from right hand end var 0..2:c1; var 0..2:c2; var 0..2:c3; var 0..2:c4; var 0..2:c5; constraint all_different ([D, E, A, R, S, T, G, H, P, Y]); % Adding columns from the right hand end constraint (R + R + Y) mod 10 == R /\ c1 == (R + R + Y) div 10; constraint (c1 + A + E + P) mod 10 == E /\ c2 == (c1 + A + E + P) div 10; constraint (c2 + E + D + P) mod 10 == T /\ c3 == (c2 + E + D + P) div 10; constraint (c3 + D + A + A) mod 10 == S /\ c4 == (c3 + D + A + A) div 10; constraint (c4 + E + H) mod 10 == A /\ c5 == (c4 + E + H) div 10; constraint E == R + c5; solve satisfy; output ["EGGS = " ++ show(EGGS) ++ "\n" ++ "([D, E, A, R, S, T, G, H, P, Y] = " ++ show([D, E, A, R, S, T, G, H, P, Y] )]; % EGGS = 4882 % ([D, E, A, R, S, T, G, H, P, Y] = % [1, 4, 0, 3, 2, 5, 8, 6, 9, 7] % ---------- % ==========LikeLike