Skip to content

Instantly share code, notes, and snippets.

@thomasms
Created December 28, 2022 21:44
Show Gist options
  • Save thomasms/baa49d429a40e9a7691be5c046a0cf0c to your computer and use it in GitHub Desktop.
Save thomasms/baa49d429a40e9a7691be5c046a0cf0c to your computer and use it in GitHub Desktop.
The Messi problem from the Guardian
"""
# https://www.theguardian.com/science/2022/dec/26/can-you-solve-it-argentinas-creative-genius
Replace the ten letters of the following sum with the ten digits 0,1,2, … 9,
such that the sum is correct.
Each letter represents a unique digit.
There are two solutions, so find the one with the largest MESSI.
MESSI
+ MESSI
= FUTBOL
F = 1
M >=5
I != 0
L in [0, 4, 6, 8]
other constraints??
Here's a brute force approach - nothing clever here :)
We could really do better though....
"""
from random import choice
from typing import Tuple
M_OPTIONS: Tuple = (5, 6, 7, 8, 9)
def guess() -> Tuple[bool, int]:
_all = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
m = choice(M_OPTIONS)
_all.remove(m)
e = choice(_all)
_all.remove(e)
s = choice(_all)
_all.remove(s)
i = choice(_all)
messi_str = f"{m}{e}{s}{s}{i}"
messi = int(messi_str)
messi_set = set(messi_str)
futbol_str = str(messi * 2)
futbol_set = set(futbol_str)
is_futbol_set = len(futbol_set) == len(futbol_str)
is_futbol_messi_diff_vals = not bool(futbol_set.intersection(messi_set))
return (
(True, messi) if is_futbol_set and is_futbol_messi_diff_vals else (False, messi)
)
def find_first() -> int:
isc, g = guess()
while not isc:
isc, g = guess()
return g
def main():
messi = find_first()
print("Found a Messi!")
print(f"MESSI={messi} FUTBOL={2*messi}")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment