g | x | w | all
Bytes Lang Time Link
01105AB1E241118T153638ZKevin Cr
010Japt180106T212829ZShaggy
036Kotlin180115T004224Zjrtapsel
010Jelly180106T233004ZMr. Xcod
025Vim + wc180114T194901ZEndenite
036Haskell180107T162947Zlynn
044Python 2180107T115948Zfeersum
030Javascript ES6180107T114154ZEndenite
013Pyth180106T211154ZMr. Xcod
069Batch180107T104345ZNeil
005Husk180107T094241ZZgarb
010Jelly180106T230710ZJonathan
017J180106T230115Zcole
048Python 2180106T214810Zovs
011Deorst180106T211832Zcaird co
009Retina180106T212924ZUriel

05AB1E, 11 bytes

3šøε`Í‚Θà}O

Input as a list of 1,2,3 for P,H,R respectively.

Try it online or verify all test cases.

Explanation:

In short:

  1. Prepend a 3 to the input
  2. Get all overlapping pairs
  3. Count the amount of pairs that has either (or both) of:
    • A leading 3
    • A trailing 1

A straight-forward version of this pseudo-code would be: 3šü2ε`Θs3Q~}O (13 bytes):
Try it online.

But this is shorter:

3š         # Prepend a 3 to the (implicit) input-list
  ø        # Create pairs with the (implicit) input-list,
           # discarding the trailing item
   ε       # Map over each pair:
    `      #  Pop and push both separately to the stack
     Í     #  Decrease the second value by 2
      ‚    #  Pair it with the first value
       Θ   #  ==1 check on both values in the pair
        à  #  Max, to check if either is truthy
   }O      # After the map: sum the list of 0s/1s together
           # (which is output implicitly as result)

Japt, 11 10 bytes

ÓUè"P?H*R?

Try it or run all test cases

ÓUè"...     :Implicit input of string U
Ó           :Bitwise NOT of the negation of
 Uè         :  Count the occurrences in U of
   "...     :    RegEx /P?H*R?/

Kotlin, 36 bytes

Regex("P?H*R?").findAll(i).count()-1

Beautified

Regex("P?H*R?").findAll(i).count()-1

Test

fun f(i:String) =
Regex("P?H*R?").findAll(i).count()-1
data class Test(val input: String, val output: Int)

val TESTS = listOf(
        Test("P", 1),
        Test("H", 1),
        Test("R", 1),
        Test("HP", 2),
        Test("RHP", 3),
        Test("HHR", 1),
        Test("PHRH", 2),
        Test("RHRPHHHR", 3),
        Test("HHHHHH", 1),
        Test("PPRRHHPP", 6),
        Test("HPPRHRPRHPPRHPPHRP", 12),
        Test("PRHRHPHHPRRRHPPRHHPPRRRHRHPRPHPRPRHHRPPPRHPRP", 28)
)

fun main(args: Array<String>) {
    for ((input, expectded) in TESTS) {
        val actual = f(input)
        if (actual != expectded) {
            throw AssertionError("$input $expectded $actual")
        }
    }
}

TIO

TryItOnline

Jelly, 10 bytes

Pn1></µƝS‘

Try it online! or Test suite! (Stolen Borrowed from Jonathan.)

Alternative:

P=1=</µƝS‘

Try it online!

Pn1></µƝS‘ | Monadic chain.

      µƝ   | Map over each pair of "neighbours" (x, y) in the list.
P          | And check whether their product...
 n1        | ... 1 if it doesn't equal 1, 0 otherwise...
   >       | Is higher than?
    </     | The pair reduced by "Smaller than?". 1 if x < y, else 0.
        S  | Sum.
         ‘ | Add 1.

Jelly, 11 bytes

Saved 1 byte with help from caird coinheringaahing.

ḅ3Ɲf⁽vḲD¤L‘

Try it online!

Vim + wc, 25 bytes

:s/P\?H*R\?/a/g␊V!wc -c␊␘

is the return key and is Ctrl+X

Try it online!

Explanation

:s/P\?H*R\?/a/g␊    Replace all button presses with the character a
V!wc -c␊␘          Count the characters using the wc command

Haskell, 36 bytes

f a=sum[1|(x,y)<-zip(2:a)a,x>1||y<1]

Try it online!

Uses the 0,1,2 encoding.

Python 2, 44 bytes

Uses P->1 H->2 R->3

lambda a:sum(1/y|x/3for x,y in zip([3]+a,a))

Javascript (ES6), 30 bytes

f=s=>s.match(/P?H*R?/g).length-1
<input id=i oninput="o.innerText=f(i.value)" value="PHHR"><pre id=o>l

Pyth, 13 bytes

tl:z"P?H*R?"3

Try it here! or Verify all the test cases.

Note that 1 also works in place of 3.

How it works?

tl:z"P?H*R?"3 | Full program. Takes input from STDIN, outputs to STDOUT.

  :z        3 | Split the input string on matches of...
    "P?H*R?"  | The regular expression "P?H*R?".
 l            | Get the length.
t             | Decrement (because splitting includes the empty string).

More about the regex:

P?     | P – The literal character P, case sensitive.
       | ? – Quantifier. Matches either one or zero times the previous character.
  H*   | H – The literal character H, case sensitive.
       | * – Quantifier. Matches any number of occurrences of the previous character.
    R? | R – The literal character R, case sensitive.
       | ? – Quantifier. Matches either one or zero times the previous character.

Batch, 69 bytes

@set/ab=2,n=0
@for %%b in (%*)do @set/an+=b/2^|!%%b,b=%%b
@echo %n%

Takes input as a list of 0-indexed command-line parameters, but you can use a list of letters p, h, r in either upper or lower case if you type set /a p=0, h=1, r=2 first. Explanation: b maintains the last input (defaulting to 2 for released) and n the count of presses. Each input adds a press if the last input was a release or the current input is a press.

Husk, 6 5 bytes

Lġo&ε

Try it online! Input is a list over 0,1,2 (the TIO link uses letters for easier copy-pasting of test cases).

Explanation

I use the same general idea as Jonathan Allan's Jelly answer: split on occurrences of the "discontinuity pairs" PP, HP, RH, RR and RP, and count the resulting blocks. In the 0,1,2 encoding, these pairs are exactly those whose left element is 2 or right element is 0.

Lġo&ε  Input is a list.
 ġ     Split between pairs that do not satisfy:
    ε  the left element is at most 1
  o&   and the right element is truthy.
L      Length.

Jelly, 10 bytes

o5ḄƝ%⁵>4S‘

A monadic chain taking a list (the P,H,R : 0,1,2 option) and returning an integer, the count.

Try it online! or see the test-suite

How?

Effectively works by getting all adjacent pairs then counting any that are not "continuation pairs" (PR, PH, HR, or HH) and adding one.

o5ḄƝ%⁵>4S‘ - Link: list of integers (in [0,1,2])  e.g.: [0,0,1,0,2,1,1,2,2,0] (representing PPHPRHHRRP)
o5         - logical OR with 5                          [5,5,1,5,2,1,1,2,2,5]
   Ɲ       - for all adjacent pairs:              i.e.: [5,5],[5,1],[1,5],[5,2],[2,1],[1,1],[1,2],[2,2],[2,5]
  Ḅ        -   convert from binary                      [ 15 ,  11 ,  7  ,  12 ,  5  ,  3  ,  4  ,  6  ,  9 ]
     ⁵     - literal ten
    %      - modulo                                     [  5 ,   1 ,  7  ,   2,   5  ,  3  ,  4  ,  6  ,  9 ]
      >4   - greater than four?                         [  1 ,   0 ,  1  ,   0,   1  ,  0  ,  0  ,  1  ,  1 ]
        S  - sum                                        5
         ‘ - increment                                  6

Previous 11 byte solution:

ḅ3Ɲạ3ḟ1,2L‘

Try it online! or see the test-suite

How?

Works like the above, but in a completely different way...

ḅ3Ɲạ3ḟ1,2L‘ - Link: list of integers (in [0,1,2])  e.g.: [0,0,1,0,2,1,1,2,2,0] (representing PPHPRHHRRP)
  Ɲ         - for all adjacent pairs:              i.e.: [0,0],[0,1],[1,0],[0,2],[2,1],[1,1],[1,2],[2,2],[2,0]
ḅ3          -   convert from base three                  [ 0  ,  1  ,  3  ,  2  ,  7  ,  4  ,  5  ,  8  ,  6 ]
   ạ3       - absolute difference with three             [ 3  ,  2  ,  0  ,  1  ,  4  ,  1  ,  2  ,  5  ,  3 ]
     ḟ1,2   - filter discard if in [1,2]                 [ 3        ,  0        ,  4              ,  5  ,  3 ]
         L  - length                                     5
          ‘ - increment                                  6

and another, again quite different:

+19*Ɲ%13ḂS‘

(add 19 to each, then for adjacent pairs perform exponentiation, modulo by 13, modulo by 2, sum and add one).

J, 18 17 bytes

-1 Thanks to @FrownyFrog

1+1#.}:(<+:1=*)}.

Takes input in the form of 0,1,2. The helper function on TIO converts the test cases to this form.

Try it online!

The logic of the comparisons may still be golfable. I'm twisting my brain into knots trying to think of more equivalent and shorter statements.

Explanation (previous solution)

1+1#.2(</+:1=*/)\]

The only difference between the current solution and the previous one is how the comparisons are generated. The current solution explicitly compares adjacent elements by offsetting the array and the previous solution compares adjacent elements by looking at infixes of 2.

1 + 1 #. 2 (</ +: 1 = */)\ ]
         2               \ ]  On infixes of 2 on the input
                  1 = */        Is the infix 1 1 (two holds)?
            </                  Is the infix x y such that x < y?
               +:               These results NORed
    1 #.                       Add all of the results together (debase to base 1)
1 +                            Add one

This would be a lot cleaner if two holds did nothing. The code takes infixes of two and checks if they are not ascending and not two holds. If this is the case, then we add one to our final count. We have to add 1 to the end since we're off by one otherwise (or you could prepend an _ or any value greater than 2).

The way it checks if the infix is two holds is by multiplying the two values together and seeing if it is one (two holds are 1 1).

Python 2, 48 bytes

f=lambda a,*l:l==()or(a>l[0]or l[0]==a!=1)+f(*l)

Try it online!

Takes 0,1,2 as input.

Deorst, 11 bytes

'P?H*R?'ggL

Try it online!

Uses Mr. Xcoder's regex

Retina, 9 bytes

1>`P?H*R?

Try it online!