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

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