Who will win the election?

32

1

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 -> Output

"AAAA" -> 1
"BBBB" -> 2
"BBAXY" -> 2
"BAXYBNXBAYXBN" -> 2
"XXAYAN" -> 1
"AAAABXXXX" -> 2
"AXNXXXXAYB" -> 1
"NANNY" -> 1
"XA" -> 1
"YAB" -> 2
"XY" -> anything (do not need to handle test cases with no victor)
"AB" -> anything (do not need to handle test cases with no victor)

Comrade SparklePony

Posted 2019-01-22T16:31:33.203

Reputation: 5 784

Can NX or NY appear? – Erik the Outgolfer – 2019-01-22T16:43:24.783

@EriktheOutgolfer Yes, but not alone. (See last valid test case) – Comrade SparklePony – 2019-01-22T16:44:25.410

So, ANNY is the same as AY? Like, do we just remove Ns? – Erik the Outgolfer – 2019-01-22T16:45:06.710

1@EriktheOutgolfer ANNY is the same as just A NN. NX and NY become NN. – Comrade SparklePony – 2019-01-22T16:46:33.157

It might be clearer to use uppercase/lowercase instead of an asterisk. – Solomon Ucko – 2019-01-22T20:37:14.850

@SolomonUcko Good idea, editing. – Comrade SparklePony – 2019-01-22T20:40:03.293

5It might be worth specifying that none is the opposite of none, if the behavior for NY in the comments is correct. – Kamil Drakari – 2019-01-22T20:48:00.980

1IMHO there should be testcases beginning with XA, XB, YA and YB. – Neil – 2019-01-22T22:36:15.210

1May input contains only 1 letters? e.g. "A", "X", "Y", "N". – tsh – 2019-01-23T09:56:52.447

2Does the output have to be two distinct values, or can we for example output any positive integer if 1 wins and any negative integer if 2 wins? – Kevin Cruijssen – 2019-01-23T15:03:18.463

@tsh Do you mean like an array of characters? If so, yes. – Comrade SparklePony – 2019-01-23T20:16:05.393

1@KevinCruijssen It can be positive/negative. (I'll clarify in the challenge). – Comrade SparklePony – 2019-01-23T20:17:01.503

@Neil Yes, adding. Thank you. – Comrade SparklePony – 2019-01-23T20:29:57.753

@ComradeSparklePony I mean, what the output for "X"? The behavior of a single "X" is confused to me. – tsh – 2019-01-24T02:35:44.417

@tsh for a single X, due to having no decided neighbors, would be N. But then no one wins. As such, you do not need to handle such a case. (I'll clarify the challenge when I get to a computer). – Comrade SparklePony – 2019-01-24T12:08:22.270

This is a really interesting logic challenge, thanks! – AJFaraday – 2019-01-24T14:36:46.063

Do we have to handle single letter cases? – Embodiment of Ignorance – 2019-01-25T16:32:59.643

Answers

9

Java 8, 153 141 135 131 129 bytes

a->{int l=a.length,t,r=0,i=-1;for(;++i<l;r+=(t=a[i]=a[i]>4?t<3?t^3:3:a[i]>3?t:a[i])>2?0:3-t*2)t=a[i>0?i-1:i<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 (>= 1) if A wins, a negative integer (<= -1) if B wins, or 0 if it's a draw.

-18 bytes thanks to @OlivierGrégoire.

Try it online.

Explanation:

a->{                      // 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<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]>4?  //        If the `i`'th item is a 5:
                  t<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]>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
             )>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>0?         //    If `i` is not the first item:
              i-1         //     Set `t` to the previous (`i-1`'th) value
             :i<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)

Kevin Cruijssen

Posted 2019-01-22T16:31:33.203

Reputation: 67 575

i=0;for(int n:a)i+=n<2?1:n<3?-1:0;return i>0; saves a few bytes bytes. – Olivier Grégoire – 2019-01-23T13:39:18.113

1Actually, i=0;for(int n:a)i+=n>2?0:3-n*2;return i>0; is even shorter. – Olivier Grégoire – 2019-01-23T13:47:49.837

@OlivierGrégoire Thanks! When I saw your first comment I was about to find something shorter, but you beat me to it with your second comment. ;) – Kevin Cruijssen – 2019-01-23T13:51:41.723

1131 bytes by merging the second loop in the first one. It doesn't feel right, though, and some test cases might have to be added... – Olivier Grégoire – 2019-01-23T14:49:01.193

@OlivierGrégoire Thanks! Been able to golf 4 more bytes by merging it some more with the temp variable. And what feels wrong about it? If you add a System.out.println(java.util.Arrays.toString(a)); after the loop you can see it changed as you would expect (imo). What kind of test case would you think results in an incorrect result and due to what part of the code? – Kevin Cruijssen – 2019-01-23T15:00:06.107

I was wrong with my comment: I felt like in a specific line, we modified a[i+1] or a[i-1] which would have lead to invalid data at some point if r is computed immediately rather than afterwards. But... it doesn't happen so no consequences! Which leads me to another golf that I'll post in a few minutes (edit: oops, no: no further golf) – Olivier Grégoire – 2019-01-23T15:05:28.887

122 bytes: a->{int l=a.length,t,r=l,i=-1;for(;++i<l;r+=~(a[i]=a[i]>4?t<3?t^3:3:a[i]>3?t:a[i])%3)t=a[i>0?i-1:i<l-1?i+1:i];return r<0;} saves 9 more bytes if I count well. The r=l comes from a golf of ~(t=a[i]=...)%3 +1. And ~t%3+1 is the mapping with the ternary values (1 -> 1, 2 -> -1, 3 -> 0). Then I continued golfing to reach the result presented. I know it'll be hard to explain, so sorry in advance ;-) – Olivier Grégoire – 2019-01-23T16:26:48.750

@OlivierGrégoire Hmm, your 122 bytes version gives an incorrect result for one of the test cases. Here is that single test case in the 131 bytes version and here is that same test case in your 122 bytes version. The conversion of the array is the same, so that's good, but the result is different. Probably because the items will not just be 1->1, 2->-1, 3->0, but can also still be 4->0 or 5->0 when it starts with multiple X/Y, and your ~t%3+1 will map 4->-1 and 5->1 instead.

– Kevin Cruijssen – 2019-01-23T17:38:45.453

Oh, I somehow thought that those 4 and 5 were replaced by a 3. My bad. – Olivier Grégoire – 2019-01-23T18:56:14.417

@OlivierGrégoire Most indeed, but not all. :) – Kevin Cruijssen – 2019-01-24T07:32:19.123

9

Perl 5, 56 80 72 65 53 bytes

+26 bytes to handle the case X or Y in first position and A or B in second. output is 1 if 1 wins empty (false value in perl) otherwise.

s/^X(.)/$1$1/,s/A\KX|B\KY|^Y(?=B)/A/|s/B\KX|A\KY|^Y(?=A)/B/&&redo;$_=y/A//>y/B//

TIO

using P and S instead of X and Y allowing to use xor operation on characters, would save some more bytes

s/(?|^(P|S)(?=(A|B))|(A|B)\K(P|S))/P^$1^$2/e&&redo;$_=y/A//>y/B//

uses a branch reset group (?|..|..), so that $1 $2 refering to corresponding group in branch. Using \0 and \3 instead of X and Y

$_=s/^\W(?=(\w))|(\w)\K\W/$1.$2^$&/e?redo:y/A//>y/B//

72 bytes

65 bytes

53 bytes

Nahuel Fouilleul

Posted 2019-01-22T16:31:33.203

Reputation: 5 582

from my last understanding they are not counted any more – Nahuel Fouilleul – 2019-01-23T06:37:50.690

This does not correctly handle X and Y at the start of the string. Try XBA and YAB. – Grimmy – 2019-01-23T12:45:55.697

@Grimy, updated – Nahuel Fouilleul – 2019-01-23T14:46:45.023

8

Haskell, 60 50 48 59 bytes

l#(v:o)|v<2=v+v#o|n<-(3-v)*l=n+n#o
_#_=0
f x=rem(x!!1)2#x>0

Uses 1 for A, -1 for B, 0 for N, 2 for X and 4 for Y. Returns True if A wins, else False.

Try it online!

On the recursive way down the input list we add 1 for every vote for A, -1 for every vote for B and 0 for "no vote". l is the last vote, v the next. If v=1, -1 or 0 (or v<2) we just add it to the sum. If v is "vote same" (X in the challenge, 2 for my solution) we keep and add l ((3-2)*l = l). If v is "vote opposite" (Y in the challenge, 4 for my solution) we first negate l ((3-4)*l = -l) and then add it. Base case is the empty list which starts the sum with 0. Recursion is started with l set to rem s 2 where s is the second element of the input list (x!!1). rem s 2 maps 1 and -1 to itself, all other values to 0. Fix votes ignore l anyway [*] and X or Y get the right neighbor if it's a fix vote. If the overall sum is positive, A wins.

[*] this makes singleton lists with fix votes like [1] work, because due to Haskell's laziness access to the second element is never evaluated. Inputs like [2] fail with error, but don't have to be considered.

nimi

Posted 2019-01-22T16:31:33.203

Reputation: 34 639

1@Grimy: thanks for pointing out. Fixed. – nimi – 2019-01-23T16:19:09.773

6

JavaScript (ES6),  78 75  73 bytes

Takes input as an array of integers with: \$0\$ = N, \$1\$ = A, \$2\$ = B, \$4\$ = Y, \$8\$ = X.

Returns \$false\$ if the first candidate wins or \$true\$ if the 2nd candidate wins.

a=>a.reduce((v,x,i)=>v+~~[,1,-1][p=x?x&3||~-x%7^(p&3||a[i+1]&3):0],p=0)<0

Try it online!

Arnauld

Posted 2019-01-22T16:31:33.203

Reputation: 111 334

4

05AB1E, 34 33 32 30 bytes

gFÐNè©2@iNx1.S-èDÄ2‹*D(‚®èNǝ]O

Uses an integer-array as input with A=-1, B=1, N=0, X=2, Y=3 and outputs a negative integer (<= -1) if A wins, a positive integer (>= 1) if B wins, or 0 if it's a draw.

Try it online or verify all test cases.

Explanation:

g             # Take the length of the (implicit) input-list
              #  i.e. [3,1,3,3,2,0,1] → 7
 F            # Loop `N` in the range [0, length):
  Ð           #  Triplicate the list at the top of the stack
              #  (which is the implicit input-list in the first iteration)
   Nè         #  Get the `N`'th item of the list
              #   i.e. [3,1,3,3,2,0,1] and `N`=0 → 3
              #   i.e. [-1,1,-1,3,2,0,1] and `N`=3 → 3
     ©        #  Store it in the register (without popping)
   2@i        #  If it's larger than or equal to 2 (so either 2 or 3):
      Nx      #   Push `N` and `N` doubled both to the stack
              #    i.e. `N`=0 → 0 and 0
              #    i.e. `N`=3 → 3 and 6
        1.S   #   Compare the double integer with 1 (-1 if N*2<1; 0 if N*2==1; 1 if N*2>1)
              #   (So this will be -1 in the first iteration, otherwise it will be 1)
              #    i.e. 0 → -1
              #    i.e. 6 → 1
           -è #   Subtract that from the index, and index it into the list
              #    i.e. `N`=0 and -1 → 1 (first item, so get the next index)
              #     → [3,1,3,3,2,0,1] and 1 → 1
              #    i.e. `N`=3 and 1 → 2 (fourth item, so get the previous index)
              #     → [-1,1,-1,3,2,0,1] and 2 → -1
      D       #   Duplicate that value
       Ä2‹    #   Check if that value is -1, 0, or 1 (abs(i) < 2) (truthy=1; falsey=0)
          *   #   And multiply that with the value
              #   (remains the same if truthy; or becomes 0 if falsey)
      D(‚     #   Pair it with its negative (-1 becomes [-1,1]; 1 becomes [1,-1])
         ®è   #   And index the `N`'th value (from the register) into it (with wraparound)
              #   (if it was a 2, it uses the unchanged (first) value of the pair;
              #    if it was a 3, it uses the negative (second) value of the pair)
              #     i.e. [1,-1] and 3 → -1
              #     i.e. [-1,1] and 3 → 1
      Nǝ      #   And replace the `N`'th value with this
              #    i.e. [3,1,3,3,2,0,1], `N`=0 and -1 → [-1,1,3,3,2,0,1]
              #    i.e. [-1,1,-1,3,2,0,1], `N`=3 and 1 → [-1,1,-1,1,2,0,1]
 ]            # Close both the if-statement and loop
  O           # Sum the modified list (which now only contains -1, 0, or 1)
              #  i.e. [-1,1,-1,1,1,0,1] → 2

Kevin Cruijssen

Posted 2019-01-22T16:31:33.203

Reputation: 67 575

3

Retina 0.8.2, 70 bytes

AY
AB
BY
BA
}`(A|B)X
$1$1
^X(A|B)|^Y[AB]
$1$1
+`N|X|Y|AB|BA

.+|(?<=B)

Try it online! Link includes test cases. Outputs 0 for a tie. Explanation:

AY
AB
BY
BA

Handle Y voters to the right of people with decided votes.

}`(A|B)X
$1$1

Handle X voters to the right of people with decided votes, and then loop back until all possible Y and X votes can be decided.

^X(A|B)|^Y[AB]
$1$1

Handle an initial X voter next to a decided vote, and also an initial Y voter next to a decided vote. As this voter will vote opposite to the decided vote, we can just delete both votes in this case.

+`N|X|Y|AB|BA

Delete any remaining no vote or undecided votes, and cancel out all pairs of opposing decided votes. Repeat until all possible votes are cancelled. In the case of a tie, nothing will be left, otherwise the remaining votes will all be of the same type.

.+|(?<=B)

Output 1 if there are any votes, but 2 if they are B votes.

Neil

Posted 2019-01-22T16:31:33.203

Reputation: 95 035

3

JavaScript (Node.js), 42 bytes

s=>s.map(c=>x+=l=c%2|l*c/2,l=s[x=1]%2)|x>1

Try it online!

Save 1 bytes, thanks to Shaggy.


  • Input as integer array where N = 0, A = -1, B = 1, X = 2, Y = -2;
  • Output 1 = Falsy, 2 = Truthy

tsh

Posted 2019-01-22T16:31:33.203

Reputation: 13 072

2Your TIO seems to output 0, 1 and 3 instead of 1 and 2? – Kevin Cruijssen – 2019-01-23T13:26:47.147

1@KevinCruijssen But OP allowed truthy vs. falsy as output if i understand correctly. Falsy means 1 won the game, and truthy means 2 won. – tsh – 2019-01-24T02:31:55.263

Ah ok, forgot 3 is truthy in JS as well. I always think of 0/1 as falsey/truthy. And since we no longer need distinct outputs, 0 = 1 wins and >= 1 = 2 wins is fine as well. So +1 from me. – Kevin Cruijssen – 2019-01-24T07:54:43.743

It looks like you could save a byte using bitwise OR, instead of logical OR. – Shaggy – 2019-02-08T23:32:38.467

@Shaggy So strange. It works. – tsh – 2019-02-09T11:21:02.060

2

Python 3 2, 125 121 117 bytes

(Thanks to Jonathan Frech)

def f(x):
    for i,v in enumerate(x):n=x[i-(i>0)];x[i]=(v>3)*n+abs(n-1)*(v<0)+x[i]*(0<v<4)
    print x.count(1)>x.count(0)

Using tab indentation

Input: list of ints where 'A'=1, 'B'=0, 'X'=4, 'N'=3, 'Y'=-1, so "AAAA" is [1, 1, 1, 1] and "XXAYAN" is [4, 4, 1, -1, 1, 3].

[{'A': 1, 'B': 0, 'X': 4, 'N': 3, 'Y': -1}[c] for c in s] will convert the strings to the needed input format.

You can Try it online! (Thanks to Jonathan Frech for the suggestion)

user24343

Posted 2019-01-22T16:31:33.203

Reputation: 261

Hello and welcome to PPCG. I would recommend using TIO, as it nicely formats your code. Furthermore, I do not quite understand your input format. You may have to ask the OP about its validity.

– Jonathan Frech – 2019-01-22T22:39:10.467

As a golfing tip, (i, i-1)[i>0] should be equivalent to i-(i>0). – Jonathan Frech – 2019-01-22T22:44:10.313

Furthermore, your ifs could probably become x[i]+=(v>3)*n+abs(n-1)*(v<0). You can then save on indentation by moving the now non-compound statement (using ;) on the same line as the for. – Jonathan Frech – 2019-01-22T22:47:40.417

@JonathanFrech Thank you very much; I hope I explained the input better – user24343 – 2019-01-23T16:43:33.473

1

Perl 5, 54 bytes

s/^\W(?=(\w))|(\w)\K\W/$1^$2^$&/e&&redo;$_=y/A//>y/B//

Try it online!

Uses A for A, B for B, N for N, \0 for X and \3 for Y (the last two being literal control chars). The trick is that A bitwise-xor \3 equals B, and vice-versa.

Grimmy

Posted 2019-01-22T16:31:33.203

Reputation: 12 521

it uses many ideas of my answer, i wasn't sure we can use non printable character as input and output, except i didn't realize the benfit of using backslash character classes – Nahuel Fouilleul – 2019-01-23T17:00:05.583

1

Javascript (ES6) - 133 bytes

a=>(i=($=_=>'AB'.search(_)+1)(a[1],o=0),[...a].map(v=>(r=['NAB','NBA']['XY'.search(x)],p=r?r[i]:v,i=$(p),o+='NA'.search(p))),o>0?1:2)

Takes in a string with the format given in the OP and returns 1 if candidate 1 won and 2 otherwise (I'll admit it, I'm even-biased).

M Dirr

Posted 2019-01-22T16:31:33.203

Reputation: 41

1

Python 2, 95 73 bytes

lambda(v):sum([l for l in[2*int(v[1]/2)]for i in v for l in[i*l**(i%2)]])

Try it online!


  • Input as integer array where N = 0, A = -2, B = 2, X = 1, Y = -1;
  • Output negative = A, 0 = draw, positive = B
  • If first input is X or Y, then 2*int(v[1]/2) maps second to itself or 0

Bug fix was required that added extra bytes, but converting to lambda thanks to @Stephen reduced it back to 95

ABridgeTooFar

Posted 2019-01-22T16:31:33.203

Reputation: 33

74 bytes by removing whitespace and changing the function to a lambda function – Stephen – 2019-02-08T19:21:32.123