Published
Edited
Jan 14, 2022
1 star
Insert cell
# JoyStick
Insert cell
Insert cell
function solution(name) {
const alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
let minMoves = name.length - 1;

const totalMove = [...name].reduce((count, el, idx) => {
if (el !== "A") return count + verticalMove(el, alphabets);

let nextIdx = idx + 1;
let countA = 0;
while (nextIdx < name.length && name.charAt(nextIdx) == "A") {
nextIdx++;
countA++;
}
let moves =
(name.charAt(0) == "A" ? 0 : (idx - 1) * 2) +
(name.length - (1 + idx + countA));
if (minMoves > moves) minMoves = moves;

return count;
}, 0);

// console.log(totalMove, minMoves);
return totalMove + minMoves;
}
Insert cell
function verticalMove(target, alphabets) {
let idx = alphabets.findIndex((el) => el === target);
return idx > 12 ? alphabets.length - idx : idx;
}
Insert cell
solution("AABBBB")
Insert cell
solution("JEROEN") // 56
Insert cell
solution("JAN") // 23
Insert cell
solution("JAZ") // 11
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more