Who will win the election? This is a challenge in which two people, 1 and 2, are running for office. People deterministically vote in certain ways in the world of 1 and 2, which can allow for the candidates to figure out the results before the election.

NOTE: this is not meant to refer to any outside elections or other political events.

Two people are running for office. We'll call these people 1 and 2. Because they both want to know if they will win the election, they decide to use their knowledge of people and some code to figure out what the result will be. Due to the want to minimize government spending, the code needs to be a short as possible.

Your task: Given a string of people based on how they are voting, output who wins the election.

There are five kinds of people in the fun and exciting world of 1 and 2:


A: people who will definitely vote for 1.
B: people who will definitely vote for 2.
X: people who will vote for whoever the person to their left will vote for. If there is no person to their left, then they vote for whoever the person at their right will vote for. If it is not clear who the person to their right is voting for, then they do not vote.
Y: people will vote the opposite of the person to their left. If there is no person to their left, then they vote opposite of whoever is at their right. If it is not clear who the person to their right is voting for, then they do not vote.
N: people who do not vote.


This is evaluated from left to right.

Example:

Whoever is being "evaluated" is in lowercase, for clarity.

Input: `XXAYAN`
        xX      Votes for whoever their friend is voting for. Their friend has not decided yet, so it is unclear, so they do not vote.
        Xx      Person to left is voting "none" so votes "none."
          a     Votes for 1
          Ay    Since person on left is voting for 1, votes for 2.
            a   Votes for 1
             n  Does not vote


Final poll:


2 people voted for 1
1 people voted for 2
3 people did not vote


1 has the most votes, so 1 wins!

Test cases:

You may use other characters or values as input and output, as long as they are distinct. (For example: numbers instead of letters, different letters, lowercase letters, truthy/falsy or positive/negative (for output), etc.)

Input -&gt; Output

"AAAA" -&gt; 1
"BBBB" -&gt; 2
"BBAXY" -&gt; 2
"BAXYBNXBAYXBN" -&gt; 2
"XXAYAN" -&gt; 1
"AAAABXXXX" -&gt; 2
"AXNXXXXAYB" -&gt; 1
"NANNY" -&gt; 1
"XA" -&gt; 1
"YAB" -&gt; 2
"XY" -&gt; anything (do not need to handle test cases with no victor)
"AB" -&gt; anything (do not need to handle test cases with no victor)
Java 8, 153 141 135 131 129 bytes

a-&gt;{int l=a.length,t,r=0,i=-1;for(;++i&lt;l;r+=(t=a[i]=a[i]&gt;4?t&lt;3?t^3:3:a[i]&gt;3?t:a[i])&gt;2?0:3-t*2)t=a[i&gt;0?i-1:i&lt;l-1?i+1:i];return r;}

Uses an integer array as input with A=1, B=2, N=3, X=4, Y=5 and outputs a positive integer (&gt;= 1) if A wins, a negative integer (&lt;= -1) if B wins, or 0 if it's a draw.
-18 bytes thanks to @OlivierGrégoire.
Try it online.
Explanation:
a-&gt;{                      // Method with int-array parameter and boolean return-type
  int l=a.length,         //  Length of the input-array
      t,                  //  Temp integer, uninitialized
      r=0,                //  Result-integer, starting at 0
  i=-1;for(;++i&lt;l         //  Loop `i` in the range [0, l):
           ;              //    After every iteration:
            r+=           //     Increase the result by:
             (t=a[i]=     //       Change `i`'th item in the array to:
                 a[i]&gt;4?  //        If the `i`'th item is a 5:
                  t&lt;3?    //         If `t` is 1 or 2:
                   t^3    //          Use `t` Bitwise-XOR 3 to invert it
                          //          (1 becomes 2; 2 becomes 1)
                  :       //         Else (`t` is 3, 4, or 5 instead):
                   3      //          Set it to 3
                 :a[i]&gt;3? //        Else-if the `i`'th item is a 4:
                  t       //         Set it to `t`
                 :        //        Else (the `i`'th item is a 1, 2 or 3):
                  a[i])   //         Leave it unchanged
             )&gt;2?         //      And if this new `i`'th value is 3, 4, or 5:
              0           //       Leave the result the same by increasing it with 0
             :            //      Else (it's 1 or 2 instead):
              3-t*2;      //       Increase it by 3 minus two times the `i`'th value
                          //       (which is 1 for 1; and -1 for 2)
         t=               //   Set `t` to:
           a[i&gt;0?         //    If `i` is not the first item:
              i-1         //     Set `t` to the previous (`i-1`'th) value
             :i&lt;l-1?      //    Else-if `i` is not the last item:
              i+1         //     Set `t` to the next (`i+1`'th) value
             :            //    Else (`i` is the first or last item):
              i];         //     Set `t` to the current item itself
  return r;}              //  Return the result
                          //  (positive if A wins; negative if B wins; 0 if it's draw)