Skip to content

Instantly share code, notes, and snippets.

@tomhebbron
Last active May 30, 2022 17:09
Show Gist options
  • Select an option

  • Save tomhebbron/8e3e6c143758971564aafdace9625a92 to your computer and use it in GitHub Desktop.

Select an option

Save tomhebbron/8e3e6c143758971564aafdace9625a92 to your computer and use it in GitHub Desktop.
Crack the padlock logic challenge - python brute force!
def rightplace(a,b,len=3):
astr = str.zfill(str(a),len)
bstr = str.zfill(str(b),len)
return [1 if astr[x]==bstr[x] else 0 for x in range(len)]
def wrongplace(a,b,len=3):
astr = str.zfill(str(a),len)
bstr = str.zfill(str(b),len)
return [1 if astr[x] in bstr and astr[x] != bstr[x] else 0 for x in range(len)]
for i in range(0,1000,1):
## 682: One number is correct and well placed.
if sum(rightplace(i,682)) != 1: continue
## 614: One number is correct but wrongly placed.
if sum(wrongplace(i,614)) != 1: continue
## 206: Two numbers are correct but wrongly placed.
if sum(wrongplace(i,206)) != 2: continue
## 738: Nothing is correct.
if sum(wrongplace(i,738)) != 0 or sum(rightplace(i,738)) != 0: continue
## 780: One number is correct but wrongly placed.
if sum(wrongplace(i,380)) != 1: continue
print(str.zfill(str(i),3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment