Advitiya
Source: codechef
Difficulty: 844

Advitiya
Toofan wanted to write the string
"ADVITIYA"
, but accidentally wrote the 8-letter string$S$instead.   He wants to correct his mistake with the minimum number of steps.
In one step, Toofan can choose an index$i$($1 \leq i \leq N$), and change$S_i$to the next letter in the alphabet.
That is, change$\text{A} \to \text{B}, \text{B} \to \text{C}, \text{C} \to \text{D}$, and so on.
The alphabet is considered cyclic, so$\text{Z}$can be changed to$\text{A}$.
For example, if$S$equals
"ZARAGOZA"
,
Operating on the first index will turn$S$into
"AARAGOZA"
.
Operating on the second index will instead turn$S$into
"ZBRAGOZA"
.
Operating on the third index will instead turn$S$into
"ZASAGOZA"
.
And so on for the other indices.
Find the minimum number of steps required to convert the string$S$into
"ADVITIYA"
.
Input Format
The first line contains an integer$T$— the number of test cases.
The next$T$lines each contain a string$S$consisting of exactly$8$uppercase English letters.
Output Format
For each test case, output a single integer on a new line — the minimum number of steps to convert$S$into
"ADVITIYA"
.
Constraints$1 \leq T \leq 1000$$S$has length$8$.
Each character of$S$is an uppercase English letter.
Sample 1:
Explanation:
Test case$1$:$S$already equals
"ADVITIYA"
, so$0$steps are needed.
Test case$2$:
"ADVITIAA"
can be turned into
"ADVITIYA"
in$24$steps, by repeatedly operating on the$7$-th character.
That is,
"ADVITIAA"
becomes
"ADVITIBA"
, then
"ADVITICA"
, then
"ADVITIDA"
, and so on till it reaches
"ADVITIYA"
.

Sample Input:
2
ADVITIYA
ADVITIAA

Sample Output:
0
24

C++ solution:
#include <bits/stdc++.h>
using namespace std;

int main() {
    // your code goes here
    int tc;
    cin>>tc;
    while(tc--){
        string s,t="ADVITIYA";
        int res=0;
        cin>>s;
        for(int i=0;i<s.size();i++){
            int diff = (t[i] - s[i] + 26) % 26;
            res += diff;
        }
        cout<<res<<endl;
    }
    return 0;
}

Go solution:
package main

import "fmt"

func minSteps(s string) int {
    target := "ADVITIYA"
    steps := 0
    for i := 0; i < 8; i++ {
        diff := int(target[i]) - int(s[i])
        if diff < 0 {
            diff += 26
        }
        steps += diff
    }
    return steps
}

func main() {
    var T int
    fmt.Scan(&T)
    for i := 0; i < T; i++ {
        var S string
        fmt.Scan(&S)
        fmt.Println(minSteps(S))
    }
}

Java solution:
import java.util.*;
import java.lang.*;
import java.io.*;

class Codechef
{
    public static void main (String[] args) throws java.lang.Exception
    {
        Scanner sc=new Scanner(System.in);
         int T = Integer.parseInt(sc.nextLine());
        String target="ADVITIYA";
        while(T-->0){
            int steps=0;
            String S=sc.nextLine().trim();
            for(int i=0; i<8; i++){
                char avl=S.charAt(i);
                char tar=target.charAt(i);
                int diff = (tar-avl + 26) % 26;  
                steps =diff+steps;
            }
            System.out.println(steps);
        }

    }
}

JavaScript solution:
// your code goes here
function minStepsToFix(inputs){
    const target = "ADVITIYA";
    let results = [];
    
    for (const word of testCases) {
        let totalSteps = 0;
        for (let i = 0; i < 8; i++) {
            let diff = (target.charCodeAt(i) - word.charCodeAt(i) + 26) % 26;
            totalSteps += diff;
            
        }
        results.push(totalSteps);
    }
    console.log(results.join("\n"));
}


const readline = require("readline");
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});


let testCases = [];
let count = -1;

rl.on("line", (line) => {
    if (count === -1) {
        count = parseInt(line.trim(), 10);
    } else {
        testCases.push(line.trim());
        if (testCases.length === count) {
            minStepsToFix(testCases);
            rl.close();
        }
    }
});

Python solution:
# cook your dish here
T= int(input())
for i in range(T):
    s = input()
    
    target = 'ADVITIYA'
    step = 0
    
    for i in range(8):
        if s[i] != target[i]:
            
            if ord(s[i]) < ord(target[i]):
                step += ( ord(target[i]) - ord(s[i]) )
                
            else:
                step += ( 26 - (ord(s[i]) - ord(target[i])) )
                
    print(step)

Ruby solution:
#!/usr/bin/env ruby

def calculate_steps(s, target)
  steps = 0
  s.chars.each_with_index do |char, index|
    current = char.ord
    desired = target[index].ord
    next if current == desired

    if current < desired
      steps += desired - current
    else
      steps += 26 - (current - desired)
    end
  end
  steps
end

# Main execution
if __FILE__ == $0
  T = gets.to_i
  target = "ADVITIYA"
  
  T.times do
    s = gets.chomp
    puts calculate_steps(s, target)
  end
end