Skip to content

Instantly share code, notes, and snippets.

@hangst
Last active April 7, 2020 13:59
Show Gist options
  • Save hangst/5d4b61f90dbd724ac0fb277e8115bc63 to your computer and use it in GitHub Desktop.
Save hangst/5d4b61f90dbd724ac0fb277e8115bc63 to your computer and use it in GitHub Desktop.
Solution to the key problem
#!/usr/bin/env python
"""
Problem statement: Find the three digit number
147 - One digit is right but in the wrong place
189 - One digit is right and in its place
964 - Two digits are correct but both are in the wrong place
523 - All digits are wrong
286 - One digit is right but in the wrong place
"""
def n(a,i): a = a[:]; del a[i]; return a # Exclude element at I from A
def q(a,b): return sum([ a[i] in n(b,i) for i,_ in enumerate(a) ]) # Total right but wrong place
def p(a,b): return sum([ a[i] == b[i] for i,_ in enumerate(a) ]) # Total right and correct place
def r(a,b,n): return q(a,b) == n and p(a,b) == 0 # N right but wrong place && 0 right and correct place
def s(a,b,n): return p(a,b) == n and q(a,b) == 0 # N right and correct place && 0 right but wrong place
for i in range(10):
for j in range(10):
for k in range(10):
if (r([i,j,k],[1,4,7], 1) and
s([i,j,k],[1,8,9], 1) and
r([i,j,k],[9,6,4], 2) and
s([i,j,k],[5,2,3], 0) and
r([i,j,k],[2,8,6], 1)): print(i,j,k)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment