DEV Community

dev.to staff
dev.to staff

Posted on

8 7

Daily Challenge #165 - Password Lost in a Grid

Setup:

Today's challenge is to recover your password from a (m * n) grid. The array will contain pieces of the password to be recovered, you'll get directions on how to get all the the pieces, your initial position in the array will be the character "x".

Examples:

Here's what the array looks like:

[
  ["p", "x", "m"],
  ["a", "$", "$"],
  ["k", "i", "t"]
]

The given directions would consist of [left, right, up, down] and [leftT, rightT, upT, downT], the former would be used to move around the grid while the latter would be used when you have a password to that direction of you.( E.g if you are in a position and the move to make is leftT it means theres a password piece to the left of you, so take the value and move there)
So in the 2d array example above, you will be given the directions ["leftT", "downT", "rightT", "rightT"], making the word "pa$$".
Remember you initial position is the character "x".
So you write the function getPassword(grid, directions) that uses the directions to get a password in the grid.

Another example:

grid = [
  ["a", "x", "c"],
  ["g", "l", "t"],
  ["o", "v", "e"]
];

directions = ["downT", "down", "leftT", "rightT", "rightT", "upT"]
getPassword(grid, directions) // => "lovet"

Once again, Your initial position is the character "x", so from the position of "x" you follow the directions given and get all pieces in the grid.

Tests

grid = [
    ["x", "l", "m"],
    ["o", "f", "c"],
    ["k", "i", "t"]
  ]
directions = ["rightT", "down", "leftT", "right", "rightT", "down", "left", "leftT"]

Good luck!


This challenge comes from kodejuice on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (3)

Collapse
 
tsanak profile image
tsanak

In Javascript

grid = [
    ["x", "l", "m"],
    ["o", "f", "c"],
    ["k", "i", "t"]
  ]
directions = ["rightT", "down", "leftT", "right", "rightT", "down", "left", "leftT"]

function getPassword(grid, directions) {
  let password = '';
  const rowLength = grid[0].length;
  // Convert grid array to string
  const gridString = grid.join(',').split(',').join('');
  let currentPosition = gridString.indexOf('x');

  directions.map(dir => {
    const lastChar = dir.substr(-1);
    if(dir.includes('right')) {
      currentPosition += 1;
    } else if(dir.includes('left')) {
      currentPosition -= 1;
    } else if(dir.includes('down')) {
      currentPosition += rowLength;
    } else {
      currentPosition -= rowLength;
    }

    if(lastChar == 'T') {
      password += gridString[currentPosition];
    }
  })

  return password;
}

let pass = getPassword(grid, directions);

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

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay