A common security method used for online banking is to ask the user for three random characters from a passcode. For example, if the passcode was 531278, they may ask for the 2nd, 3rd, and 5th characters; the expected reply would be: 317.
The text file, keylog.txt, contains fifty successful login attempts.
Given that the three characters are always asked for in order, analyse the file so as to determine the shortest possible secret passcode of unknown length.
Iterate through all logins, find a digit exists in the first digit of all logins but not in second or third digits, then remove this digit from all first digit of all occurance.
import sys, os; sys.path.append(os.path.abspath('..'))
from timer import timethis
from urllib.request import urlopen
with urlopen('https://projecteuler.net/project/resources/p079_keylog.txt') as f:
resp = f.read().decode('utf-8')
logins = resp.splitlines()
@timethis
def solve():
global logins
passcode = []
while ''.join(logins):
start_digits = set()
for login in logins:
if login[0] not in ''.join([login[1:] for login in logins]):
start_digits.add(login[0])
d = start_digits.pop()
passcode.append(d)
for i, login in enumerate(logins):
if login.startswith(d):
logins[i] = login[1:]
logins = list(filter(None, logins))
return int(''.join(passcode))
solve()