My parents have an home theater device. The remote is broken making it incredibly difficult to navigate rightwards in a menu. Most the time it doesn't work but when it does it moves rightwards incredibly quickly.

This is obviously frustrating but it is most frustrating when you want to enter a movie title which requires navigating a keyboard that looks like this:

a b c d e f
g h i j k l
m n o p q r
s t u v w x
y z 1 2 3 4
5 6 7 8 9 0

Your task is to take as input a movie title and calculate how "frustrating" it is to type that movie title. The frustration number of a particular string is the number of letters that require moving right from the letter before them. We don't care how far right they are, since if we start moving right we pretty much instantly go to the end of the line, and we don't care about up, down or leftwards movement because they are easy.

For example if we wanted to type in

keyboard
  • We start at k for free.
  • e is just above k so we don't need to move right.
  • y is all the way left so no need to move right.
  • b however is on the next column rightwards so we need to move right to get to it.
  • o is on the next column over so we have to move rightwards to get to it.
  • a is back in the first column so we move left to get to it.
  • r is all the way on the right so we move right to it.
  • d is two columns to the left of r's column.

The characters that need to move to the right are bor meaning that this is frustration 3.

Additional rules

This is a challenge so your answers will be scored in bytes with fewer bytes being better. The input will always consist of alphanumeric characters, you can support either capital or lowercase letters and you only need to support one. The input will never be empty.

Testcases

keyboard -> 3
2001aspaceodyssey -> 6
sorrytobotheryou -> 8
thinblueline -> 5
blast2 -> 3
  • 2
    Suggested test case: "blast2" -> 3 (not a real movie, but some answers have problems with such test cases) – Arnauld Nov 24 at 17:37
  • Suggested test case: one consisting of only digits, such as 5 -> 0 – lirtosiast Nov 24 at 18:01
  • 1
    Suggested test case: 90 -> 1 – nwellnhof Nov 24 at 20:46
  • Can we assume the input string will be non-empty? – Chas Brown Nov 25 at 4:06
  • @ChasBrown That is covered in the question. – Post Left Garf Hunter Nov 25 at 4:17

14 Answers 14

JavaScript (Node.js), 61 55 54 bytes

Saved 1 byte thanks to @nwellnhof

Takes input as an array of characters.

s=>s.map(p=c=>r+=p>(p=(+c?~c:1-Buffer(c)[0])%6),r=0)|r

Try it online!

How?

For all characters but digits greater than \$0\$, the 0-indexed column \$x\$ is given by:

$$x=(c-1)\bmod 6$$

where \$c\$ is the ASCII code of the character.

For positive digits \$n\$, we need to do instead:

$$x=(n+1)\bmod 6$$

Examples:

"a" --> (97 - 1) mod 6 = 96 mod 6 = 0
"b" --> (98 - 1) mod 6 = 97 mod 6 = 1
"0" --> (48 - 1) mod 6 = 47 mod 6 = 5
"3" --> ( 3 + 1) mod 6 =  4 mod 6 = 4

Commented

s =>                       // s = input string (as array)
  s.map(p =                // initialize p to a non-numeric value
  c =>                     // for each character c in s:
    r +=                   //   update the result r:
      p > (                //   compare p with
        p = (              //   the new value of p defined as:
          +c ?             //     if c is a positive digit:
            ~c             //       -(int(c) + 1)
          :                //     else:
            1-Buffer(c)[0] //       -(ord(c) - 1)
        ) % 6              //     apply modulo 6
      ),                   //   yields 1 if the previous value is greater than the new one
    r = 0                  //   start with r = 0
  ) | r                    // end of map(); return r
  • It seems to work without the ternary for 46 bytes – Shaggy Nov 24 at 17:51
  • 1
    @Shaggy It won't. See my suggested test case "blast2". – Arnauld Nov 24 at 17:52
  • Ah. In that case: 53 bytes – Shaggy Nov 24 at 18:29
  • 1
    @Shaggy A bitwise OR would fail for, say, "234". – Arnauld Nov 24 at 18:33
  • 3
    Less whiskey is never the answer! – Shaggy Nov 24 at 18:58

Jelly, 11 bytes

⁾04yO‘%6<ƝS

A monadic Link accepting a list of (uppercase) characters.

Try it online!

How?

First replaces any '0's with '4's (so the rest of the code treats them as being in the rightmost column). Then casts to ordinals, adds one and modulo's by 6 to get 0-based column indices. Then compares neighbours with is-less-than and sums the result.

⁾04yO‘%6<ƝS - Link: list of characters         e.g. "BLAST20"
⁾04         - list of characters = ['0', '4']
   y        - translate                             "BLAST24"
    O       - ordinals                              [66,76,65,83,84,50,52]
     ‘      - increment                             [67,77,66,84,85,51,53]
       6    - literal six
      %     - modulo                                [ 1, 5, 0, 0, 1, 3, 5]
         Ɲ  - neighbourly:
        <   -   less than?                          [  1, 0, 0, 1, 1, 1  ]
          S - sum                                   4

Perl 6, 45 39 bytes

{sum .[1..*]Z<$_}o{(2 X-.ords)X%46 X%6}

Try it online!

Works with uppercase letters. (2-ord(c))%46%6 computes the reversed x coordinate.

K (ngn/k), 32 31 bytes

+/>':(+6 6#,/"a10"+!'26 9 1)?0+

Try it online!

Clean, 85 bytes

import StdEnv
(\s=sum[1\\a<-s&b<-tl s|b>a])o map(\e=(toInt e+if(e-'1'>'/')1 -1)rem 6)

Try it online!

Ruby, 56 bytes

->s{w=9;s.count{|c|w<w=[*?a..?z,*?1..?9,?0].index(c)%6}}

Try it online!

Preliminary naive version, will be golfed.

Japt -x, 14 bytes

®rT4 c Ä u6Ãä<

Try it online!

Port of this Jelly answer. Takes input as an array of characters, with letters uppercase.

Explanation:

®rT4 c Ä u6Ãä<    :
®          Ã      :Map each character through:
 rT4              : Replace 0 with 4
     c            : Get the char-code
       Ä          : Increment it
         u6       : Modulo 6
            ä<    :Replace with 1 if you had to move right, 0 otherwise
                  :Implicitly sum and output

Java (OpenJDK 8), 73 bytes

Not a bad solution for Java! That zero being on the right-hand side cost me several bytes.

t->{int a=9,c=0;for(int d:t)c+=a<(a=(--d+(d/48==1?2:0))%6)?1:0;return c;}

Try it online!

Explained

t -> {                          // Lambda taking a char array as input
    int a=9,                    // Initialise last column value
        c=0;                    // Initialise frustration count
    for(int d:t)                // Loop through all chars in title
        c+=                     // increment the frustration count if...
          a<                    // The last column is smaller than the current column
            (a=                 // Set last column to current column
              (--d+             // Decrement ascii value of char
                  (d/48==1      // If ascii decremented ascii value is between 48 and 95
                    ?2:0)       // increment by 2 (1 total) or 0 (-1 total)
                )%6)            // Mod 6 to retrieve column index
            ?1:0;               // Increment if to right hand side
    return c;                   // return calculated frustration count
}

05AB1E, 12 11 bytes

-1 byte thanks to @Kevin Cruijssen

¾4:Ç>6%¥1@O

Another port of Jonathan Allan's Jelly answer. Takes input in uppercase.

Explanation:

¾4:Ç>6%¥1@O   //full program
¾4:           //replace all '0's with '4's
   Ç          //get ASCII code points
    >         //increment
     6%       //modulo 6
       ¥      //get deltas
        1@    //is >= 1
          O   //sum

Try it online!

Retina 0.8.2, 46 bytes

T`l1-90`1-61-61-61-61-61-6
.
;$&$*
&`;(1+);1\1

Try it online! Link includes test cases. Explanation:

T`l1-90`1-61-61-61-61-61-6

List the alphabet and digits in the order on the OSK and map each one to a (1-indexed) column number.

.
;$&$*

Convert each column number to unary.

&`;(1+);1\1

Count the number of columns that are followed by a larger (i.e. rightwards) column. The &` allows the matches to overlap.

Python 2, 84 bytes

def f(s,k=6):j=(ord(s[0])+('0'<s[0]<':')*2-1)%6;return(j>k)+(~-len(s)and f(s[1:],j))

Try it online!

Mathematica, 102 bytes

Differences[Last@@Join[Alphabet[],ToString/@Range@9,{"0"}]~Partition~6~Position~#&/@#]~Count~_?(#>0&)&

Pure function. Takes a list of characters as input and returns a number as output. This is a pretty naive solution, golfing suggestions welcome.

PHP, 74 81 77 bytes

for(;$o=ord($argn[$i]);$i++&&$f+=$p<$x,$p=$x)$x=(--$o/48^1?$o:$o+2)%6;echo$f;

Run as pipe with -nR or try it online.

C (gcc),  82 79  77 bytes

o;c(i){i+=i<60&i>48?1:5;i%=6;}f(char*s){for(o=0;*++s;o+=c(*s)>c(s[-1]));o=o;}

Try it online!

This function will only support lowercase inputs


Ungolfed and commented:

o; //Used for output
c(i){             //Calculates the column of given character
     i+=          //Correct i to get the correct column
        i<60      //If i is a digit...
        & i>48   //... but not '0'
        ?1           //Then move it one column on the right
        :5;          //Else move it five columns on the right
     i%=6;        //Get the column number
}
f(char*s){                        // The actual "frustrating" function
          for(                    //Loop for each character
              o=0;                //reinitialize output
              *++s;               //move to next character / while this is not '\0'
              o+=c(*s)>c(s[-1])  //Increment if current character is on the right of the previous one
             );
           o=o;                   // Outputs result
}

If my function is allowed to accept wide character strings, it can be reduced to 76 bytes with:

o;c(i){i+=i<60&i>48?1:5;i%=6;}f(int*s){for(o=0;*++s;o+=c(*s)>c(s[-1]));o=o;}

Try it online!

This version just accept input as int* instead of char*


Edits:

  • Golfed 3 bytes in the calculation of the column (function c)
  • Golfed 2 bytes thanks to ceilingcat

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.