Created
January 21, 2022 20:23
-
-
Save peio/fe3c893f8fee5609318011a19f2335ef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
"""Solve wordle with primes: https://converged.yt/primel/ """ | |
import random | |
def five_digit_primes(): | |
"Return all five digit primes" | |
'Source: https://www.quora.com/What-is-5-digit-maximum-prime-number-And-how-did-you-find-it' | |
p = [True]*1000005 | |
for x in range(2,1001): | |
if p[x]: | |
for y in range(x*2,1000000,x): | |
p[y]=False | |
primes = [x for x in range(10000,100000) if p[x]] | |
return primes | |
def optimal_guess(primes): | |
"Guess a good candidate" | |
'This is the initial state, do not fool around' | |
if len(primes) == 8363: | |
return 12037 | |
"Next iterations" | |
candidates = [] | |
for prime in primes: | |
digits = [int(x) for x in str(prime)] | |
"There are more primes starting with 1 so more likely" | |
if (len(set(digits)) == 5) and (digits[0] == 1): | |
return prime | |
else: | |
candidates.append(prime) | |
if len(candidates) > 0: | |
return random.choice(candidates) | |
else: | |
return random.choice(primes) | |
loop_counter = 0 # To avoid endless while | |
primes = five_digit_primes() | |
daily_primel = random.choice(primes) | |
print("The daily primel:",daily_primel) | |
daily_primel = [int(x) for x in str(daily_primel)] | |
while len(primes) > 1: | |
guess = optimal_guess(primes) | |
print("guess",loop_counter+1,":",guess) | |
guess = [int(x) for x in str(guess)] | |
# print (len(primes)) | |
for i, (x, y) in enumerate(zip(guess, daily_primel)): | |
if x == y: | |
print ('match', x) | |
primes = [prime for prime in primes if x == [int(a) for a in str(prime)][i] ] | |
elif x in daily_primel: | |
print ('present',x) | |
primes = [prime for prime in primes if str(x) in str(prime) and x != [int(a) for a in str(prime)][i] ] | |
else: | |
print ('absent', x) | |
primes = [prime for prime in primes if str(x) not in str(prime)] | |
loop_counter += 1 | |
"Looping prevention" | |
if loop_counter > 10: | |
break | |
print (daily_primel,primes) | |
print ("Total cycles:",loop_counter) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment