DEV Community

Discussion on: Daily Challenge #165 - Password Lost in a Grid

Collapse
 
amcmillan01 profile image
Andrei McMillan

python

def getPassword(grid, directions):
    entry = {}
    password = ''

    for row, a in enumerate(grid):

        try:
            col = a.index('x')
        except:
            col = -1

        if col > -1:
            entry['row'] = row
            entry['col'] = col

    if 'row' in entry:

        for d in directions:

            if d in ['down', 'downT']:
                entry['row'] += 1

            if d in ['up', 'upT']:
                entry['row'] -= 1

            if d in ['left', 'leftT']:
                entry['col'] -= 1

            if d in ['right', 'rightT']:
                entry['col'] += 1

            if d in ['downT', 'upT', 'leftT', 'rightT']:
                password += grid[entry['row']][entry['col']]

    print password
# test 1
grid = [
    ["p", "x", "m"],
    ["a", "$", "$"],
    ["k", "i", "t"],
]
directions = ["leftT", "downT", "rightT", "rightT"]
getPassword(grid, directions) # pa$$
# test 2
grid = [
    ["a", "x", "c"],
    ["g", "l", "t"],
    ["o", "v", "e"]
]
directions = ["downT", "down", "leftT", "rightT", "rightT", "upT"]
getPassword(grid, directions) # lovet