Public
Edited
Aug 28, 2023
Insert cell
Insert cell
entryTime = {
function distance(matrix, char1, char2) {
const [x1, y1] = matrix[char1];
const [x2, y2] = matrix[char2];
return Math.abs(x1 - x2) + Math.abs(y1 - y2);
}
function entryTime(input, matrix) {
let current = input[0];
let elapsed = 0;
for (let i = 1; i < input.length; i++) {
elapsed += distance(matrix, current, input[i]);
current = input[i];
}
return elapsed;
}

function toMatrix(keypad, rows = 4, cols = 4) {
const matrix = {};
for (let i = 0; i < keypad.length; i++) {
matrix[keypad[i]] = [i % cols, Math.floor(i / rows)];
}
return matrix;
}

return function solution(keypad, password) {
const matrix = toMatrix(keypad);
return entryTime(password, matrix);
}
}
Insert cell
entryTime("0123456789abcdef", "15ebb")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more