Who will win the election?
$begingroup$
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)
code-golf string decision-problem
$endgroup$
|
show 12 more comments
$begingroup$
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)
code-golf string decision-problem
$endgroup$
1
$begingroup$
@EriktheOutgolfer ANNY is the same as just A NN. NX and NY become NN.
$endgroup$
– Comrade SparklePony
Jan 22 at 16:46
5
$begingroup$
It might be worth specifying thatnone
is the opposite ofnone
, if the behavior forNY
in the comments is correct.
$endgroup$
– Kamil Drakari
Jan 22 at 20:48
1
$begingroup$
IMHO there should be testcases beginning withXA
,XB
,YA
andYB
.
$endgroup$
– Neil
Jan 22 at 22:36
1
$begingroup$
May input contains only 1 letters? e.g. "A", "X", "Y", "N".
$endgroup$
– tsh
Jan 23 at 9:56
2
$begingroup$
Does 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?
$endgroup$
– Kevin Cruijssen
Jan 23 at 15:03
|
show 12 more comments
$begingroup$
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)
code-golf string decision-problem
$endgroup$
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)
code-golf string decision-problem
code-golf string decision-problem
edited Mar 3 at 22:48
Embodiment of Ignorance
1,578124
1,578124
asked Jan 22 at 16:31


Comrade SparklePonyComrade SparklePony
3,43611554
3,43611554
1
$begingroup$
@EriktheOutgolfer ANNY is the same as just A NN. NX and NY become NN.
$endgroup$
– Comrade SparklePony
Jan 22 at 16:46
5
$begingroup$
It might be worth specifying thatnone
is the opposite ofnone
, if the behavior forNY
in the comments is correct.
$endgroup$
– Kamil Drakari
Jan 22 at 20:48
1
$begingroup$
IMHO there should be testcases beginning withXA
,XB
,YA
andYB
.
$endgroup$
– Neil
Jan 22 at 22:36
1
$begingroup$
May input contains only 1 letters? e.g. "A", "X", "Y", "N".
$endgroup$
– tsh
Jan 23 at 9:56
2
$begingroup$
Does 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?
$endgroup$
– Kevin Cruijssen
Jan 23 at 15:03
|
show 12 more comments
1
$begingroup$
@EriktheOutgolfer ANNY is the same as just A NN. NX and NY become NN.
$endgroup$
– Comrade SparklePony
Jan 22 at 16:46
5
$begingroup$
It might be worth specifying thatnone
is the opposite ofnone
, if the behavior forNY
in the comments is correct.
$endgroup$
– Kamil Drakari
Jan 22 at 20:48
1
$begingroup$
IMHO there should be testcases beginning withXA
,XB
,YA
andYB
.
$endgroup$
– Neil
Jan 22 at 22:36
1
$begingroup$
May input contains only 1 letters? e.g. "A", "X", "Y", "N".
$endgroup$
– tsh
Jan 23 at 9:56
2
$begingroup$
Does 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?
$endgroup$
– Kevin Cruijssen
Jan 23 at 15:03
1
1
$begingroup$
@EriktheOutgolfer ANNY is the same as just A NN. NX and NY become NN.
$endgroup$
– Comrade SparklePony
Jan 22 at 16:46
$begingroup$
@EriktheOutgolfer ANNY is the same as just A NN. NX and NY become NN.
$endgroup$
– Comrade SparklePony
Jan 22 at 16:46
5
5
$begingroup$
It might be worth specifying that
none
is the opposite of none
, if the behavior for NY
in the comments is correct.$endgroup$
– Kamil Drakari
Jan 22 at 20:48
$begingroup$
It might be worth specifying that
none
is the opposite of none
, if the behavior for NY
in the comments is correct.$endgroup$
– Kamil Drakari
Jan 22 at 20:48
1
1
$begingroup$
IMHO there should be testcases beginning with
XA
, XB
, YA
and YB
.$endgroup$
– Neil
Jan 22 at 22:36
$begingroup$
IMHO there should be testcases beginning with
XA
, XB
, YA
and YB
.$endgroup$
– Neil
Jan 22 at 22:36
1
1
$begingroup$
May input contains only 1 letters? e.g. "A", "X", "Y", "N".
$endgroup$
– tsh
Jan 23 at 9:56
$begingroup$
May input contains only 1 letters? e.g. "A", "X", "Y", "N".
$endgroup$
– tsh
Jan 23 at 9:56
2
2
$begingroup$
Does 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?
$endgroup$
– Kevin Cruijssen
Jan 23 at 15:03
$begingroup$
Does 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?
$endgroup$
– Kevin Cruijssen
Jan 23 at 15:03
|
show 12 more comments
11 Answers
11
active
oldest
votes
$begingroup$
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)
$endgroup$
$begingroup$
i=0;for(int n:a)i+=n<2?1:n<3?-1:0;return i>0;
saves a few bytes bytes.
$endgroup$
– Olivier Grégoire
Jan 23 at 13:39
1
$begingroup$
Actually,i=0;for(int n:a)i+=n>2?0:3-n*2;return i>0;
is even shorter.
$endgroup$
– Olivier Grégoire
Jan 23 at 13:47
$begingroup$
@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. ;)
$endgroup$
– Kevin Cruijssen
Jan 23 at 13:51
1
$begingroup$
131 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...
$endgroup$
– Olivier Grégoire
Jan 23 at 14:49
$begingroup$
@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 aSystem.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?
$endgroup$
– Kevin Cruijssen
Jan 23 at 15:00
|
show 5 more comments
$begingroup$
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.
$endgroup$
$begingroup$
This does not correctly handle X and Y without left neighbors
$endgroup$
– Grimy
Jan 23 at 14:52
1
$begingroup$
@Grimy: thanks for pointing out. Fixed.
$endgroup$
– nimi
Jan 23 at 16:19
add a comment |
$begingroup$
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/AKX|BKY|^Y(?=B)/A/|s/BKX|AKY|^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 and
3
instead of X
and Y
$_=s/^W(?=(w))|(w)KW/$1.$2^$&/e?redo:y/A//>y/B//
72 bytes
65 bytes
53 bytes
$endgroup$
1
$begingroup$
Did you forget some command line options?-pe
for instance?
$endgroup$
– Abigail
Jan 23 at 1:57
$begingroup$
from my last understanding they are not counted any more
$endgroup$
– Nahuel Fouilleul
Jan 23 at 6:37
3
$begingroup$
Not counted, but without them, the program doesn't do what's being asked. You can't expect the reader to know which switches your program depends on. Also, the switches are part of language (for the purpose of the contest), so you can have a different winner forperl
,perl -p
, etc.
$endgroup$
– Abigail
Jan 23 at 12:17
1
$begingroup$
Also, you can save 2 bytes by using$_=y/A//<=>y/B//
, outputting -1/0/1 for a win for A, a tie, or a win for B.
$endgroup$
– Abigail
Jan 23 at 12:21
$begingroup$
This does not correctly handleX
andY
at the start of the string. TryXBA
andYAB
.
$endgroup$
– Grimy
Jan 23 at 12:45
|
show 1 more comment
$begingroup$
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!
$endgroup$
add a comment |
$begingroup$
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
$endgroup$
add a comment |
$begingroup$
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.
$endgroup$
add a comment |
$begingroup$
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
$endgroup$
2
$begingroup$
Your TIO seems to output0
,1
and3
instead of1
and2
?
$endgroup$
– Kevin Cruijssen
Jan 23 at 13:26
1
$begingroup$
@KevinCruijssen But OP allowed truthy vs. falsy as output if i understand correctly. Falsy means 1 won the game, and truthy means 2 won.
$endgroup$
– tsh
Jan 24 at 2:31
$begingroup$
Ah ok, forgot3
is truthy in JS as well. I always think of0
/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.
$endgroup$
– Kevin Cruijssen
Jan 24 at 7:54
$begingroup$
It looks like you could save a byte using bitwise OR, instead of logical OR.
$endgroup$
– Shaggy
Feb 8 at 23:32
$begingroup$
@Shaggy So strange. It works.
$endgroup$
– tsh
Feb 9 at 11:21
add a comment |
$begingroup$
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 int
s 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)
$endgroup$
$begingroup$
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.
$endgroup$
– Jonathan Frech
Jan 22 at 22:39
$begingroup$
As a golfing tip,(i, i-1)[i>0]
should be equivalent toi-(i>0)
.
$endgroup$
– Jonathan Frech
Jan 22 at 22:44
$begingroup$
Furthermore, yourif
s could probably becomex[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 thefor
.
$endgroup$
– Jonathan Frech
Jan 22 at 22:47
$begingroup$
@JonathanFrech Thank you very much; I hope I explained the input better
$endgroup$
– user24343
Jan 23 at 16:43
add a comment |
$begingroup$
Perl 5, 54 bytes
s/^W(?=(w))|(w)KW/$1^$2^$&/e&&redo;$_=y/A//>y/B//
Try it online!
Uses A
for A
, B
for B
, N
for N
, 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.
$endgroup$
$begingroup$
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
$endgroup$
– Nahuel Fouilleul
Jan 23 at 17:00
add a comment |
$begingroup$
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).
$endgroup$
add a comment |
$begingroup$
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
$endgroup$
$begingroup$
74 bytes by removing whitespace and changing the function to a lambda function
$endgroup$
– Stephen
Feb 8 at 19:21
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "200"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f178996%2fwho-will-win-the-election%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
11 Answers
11
active
oldest
votes
11 Answers
11
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
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)
$endgroup$
$begingroup$
i=0;for(int n:a)i+=n<2?1:n<3?-1:0;return i>0;
saves a few bytes bytes.
$endgroup$
– Olivier Grégoire
Jan 23 at 13:39
1
$begingroup$
Actually,i=0;for(int n:a)i+=n>2?0:3-n*2;return i>0;
is even shorter.
$endgroup$
– Olivier Grégoire
Jan 23 at 13:47
$begingroup$
@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. ;)
$endgroup$
– Kevin Cruijssen
Jan 23 at 13:51
1
$begingroup$
131 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...
$endgroup$
– Olivier Grégoire
Jan 23 at 14:49
$begingroup$
@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 aSystem.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?
$endgroup$
– Kevin Cruijssen
Jan 23 at 15:00
|
show 5 more comments
$begingroup$
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)
$endgroup$
$begingroup$
i=0;for(int n:a)i+=n<2?1:n<3?-1:0;return i>0;
saves a few bytes bytes.
$endgroup$
– Olivier Grégoire
Jan 23 at 13:39
1
$begingroup$
Actually,i=0;for(int n:a)i+=n>2?0:3-n*2;return i>0;
is even shorter.
$endgroup$
– Olivier Grégoire
Jan 23 at 13:47
$begingroup$
@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. ;)
$endgroup$
– Kevin Cruijssen
Jan 23 at 13:51
1
$begingroup$
131 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...
$endgroup$
– Olivier Grégoire
Jan 23 at 14:49
$begingroup$
@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 aSystem.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?
$endgroup$
– Kevin Cruijssen
Jan 23 at 15:00
|
show 5 more comments
$begingroup$
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)
$endgroup$
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)
edited Jan 24 at 7:46
answered Jan 22 at 16:42


Kevin CruijssenKevin Cruijssen
40.2k563208
40.2k563208
$begingroup$
i=0;for(int n:a)i+=n<2?1:n<3?-1:0;return i>0;
saves a few bytes bytes.
$endgroup$
– Olivier Grégoire
Jan 23 at 13:39
1
$begingroup$
Actually,i=0;for(int n:a)i+=n>2?0:3-n*2;return i>0;
is even shorter.
$endgroup$
– Olivier Grégoire
Jan 23 at 13:47
$begingroup$
@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. ;)
$endgroup$
– Kevin Cruijssen
Jan 23 at 13:51
1
$begingroup$
131 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...
$endgroup$
– Olivier Grégoire
Jan 23 at 14:49
$begingroup$
@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 aSystem.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?
$endgroup$
– Kevin Cruijssen
Jan 23 at 15:00
|
show 5 more comments
$begingroup$
i=0;for(int n:a)i+=n<2?1:n<3?-1:0;return i>0;
saves a few bytes bytes.
$endgroup$
– Olivier Grégoire
Jan 23 at 13:39
1
$begingroup$
Actually,i=0;for(int n:a)i+=n>2?0:3-n*2;return i>0;
is even shorter.
$endgroup$
– Olivier Grégoire
Jan 23 at 13:47
$begingroup$
@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. ;)
$endgroup$
– Kevin Cruijssen
Jan 23 at 13:51
1
$begingroup$
131 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...
$endgroup$
– Olivier Grégoire
Jan 23 at 14:49
$begingroup$
@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 aSystem.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?
$endgroup$
– Kevin Cruijssen
Jan 23 at 15:00
$begingroup$
i=0;for(int n:a)i+=n<2?1:n<3?-1:0;return i>0;
saves a few bytes bytes.$endgroup$
– Olivier Grégoire
Jan 23 at 13:39
$begingroup$
i=0;for(int n:a)i+=n<2?1:n<3?-1:0;return i>0;
saves a few bytes bytes.$endgroup$
– Olivier Grégoire
Jan 23 at 13:39
1
1
$begingroup$
Actually,
i=0;for(int n:a)i+=n>2?0:3-n*2;return i>0;
is even shorter.$endgroup$
– Olivier Grégoire
Jan 23 at 13:47
$begingroup$
Actually,
i=0;for(int n:a)i+=n>2?0:3-n*2;return i>0;
is even shorter.$endgroup$
– Olivier Grégoire
Jan 23 at 13:47
$begingroup$
@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. ;)
$endgroup$
– Kevin Cruijssen
Jan 23 at 13:51
$begingroup$
@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. ;)
$endgroup$
– Kevin Cruijssen
Jan 23 at 13:51
1
1
$begingroup$
131 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...
$endgroup$
– Olivier Grégoire
Jan 23 at 14:49
$begingroup$
131 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...
$endgroup$
– Olivier Grégoire
Jan 23 at 14:49
$begingroup$
@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?$endgroup$
– Kevin Cruijssen
Jan 23 at 15:00
$begingroup$
@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?$endgroup$
– Kevin Cruijssen
Jan 23 at 15:00
|
show 5 more comments
$begingroup$
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.
$endgroup$
$begingroup$
This does not correctly handle X and Y without left neighbors
$endgroup$
– Grimy
Jan 23 at 14:52
1
$begingroup$
@Grimy: thanks for pointing out. Fixed.
$endgroup$
– nimi
Jan 23 at 16:19
add a comment |
$begingroup$
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.
$endgroup$
$begingroup$
This does not correctly handle X and Y without left neighbors
$endgroup$
– Grimy
Jan 23 at 14:52
1
$begingroup$
@Grimy: thanks for pointing out. Fixed.
$endgroup$
– nimi
Jan 23 at 16:19
add a comment |
$begingroup$
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.
$endgroup$
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.
edited Jan 23 at 16:18
answered Jan 22 at 17:46
niminimi
32.3k32388
32.3k32388
$begingroup$
This does not correctly handle X and Y without left neighbors
$endgroup$
– Grimy
Jan 23 at 14:52
1
$begingroup$
@Grimy: thanks for pointing out. Fixed.
$endgroup$
– nimi
Jan 23 at 16:19
add a comment |
$begingroup$
This does not correctly handle X and Y without left neighbors
$endgroup$
– Grimy
Jan 23 at 14:52
1
$begingroup$
@Grimy: thanks for pointing out. Fixed.
$endgroup$
– nimi
Jan 23 at 16:19
$begingroup$
This does not correctly handle X and Y without left neighbors
$endgroup$
– Grimy
Jan 23 at 14:52
$begingroup$
This does not correctly handle X and Y without left neighbors
$endgroup$
– Grimy
Jan 23 at 14:52
1
1
$begingroup$
@Grimy: thanks for pointing out. Fixed.
$endgroup$
– nimi
Jan 23 at 16:19
$begingroup$
@Grimy: thanks for pointing out. Fixed.
$endgroup$
– nimi
Jan 23 at 16:19
add a comment |
$begingroup$
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/AKX|BKY|^Y(?=B)/A/|s/BKX|AKY|^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 and
3
instead of X
and Y
$_=s/^W(?=(w))|(w)KW/$1.$2^$&/e?redo:y/A//>y/B//
72 bytes
65 bytes
53 bytes
$endgroup$
1
$begingroup$
Did you forget some command line options?-pe
for instance?
$endgroup$
– Abigail
Jan 23 at 1:57
$begingroup$
from my last understanding they are not counted any more
$endgroup$
– Nahuel Fouilleul
Jan 23 at 6:37
3
$begingroup$
Not counted, but without them, the program doesn't do what's being asked. You can't expect the reader to know which switches your program depends on. Also, the switches are part of language (for the purpose of the contest), so you can have a different winner forperl
,perl -p
, etc.
$endgroup$
– Abigail
Jan 23 at 12:17
1
$begingroup$
Also, you can save 2 bytes by using$_=y/A//<=>y/B//
, outputting -1/0/1 for a win for A, a tie, or a win for B.
$endgroup$
– Abigail
Jan 23 at 12:21
$begingroup$
This does not correctly handleX
andY
at the start of the string. TryXBA
andYAB
.
$endgroup$
– Grimy
Jan 23 at 12:45
|
show 1 more comment
$begingroup$
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/AKX|BKY|^Y(?=B)/A/|s/BKX|AKY|^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 and
3
instead of X
and Y
$_=s/^W(?=(w))|(w)KW/$1.$2^$&/e?redo:y/A//>y/B//
72 bytes
65 bytes
53 bytes
$endgroup$
1
$begingroup$
Did you forget some command line options?-pe
for instance?
$endgroup$
– Abigail
Jan 23 at 1:57
$begingroup$
from my last understanding they are not counted any more
$endgroup$
– Nahuel Fouilleul
Jan 23 at 6:37
3
$begingroup$
Not counted, but without them, the program doesn't do what's being asked. You can't expect the reader to know which switches your program depends on. Also, the switches are part of language (for the purpose of the contest), so you can have a different winner forperl
,perl -p
, etc.
$endgroup$
– Abigail
Jan 23 at 12:17
1
$begingroup$
Also, you can save 2 bytes by using$_=y/A//<=>y/B//
, outputting -1/0/1 for a win for A, a tie, or a win for B.
$endgroup$
– Abigail
Jan 23 at 12:21
$begingroup$
This does not correctly handleX
andY
at the start of the string. TryXBA
andYAB
.
$endgroup$
– Grimy
Jan 23 at 12:45
|
show 1 more comment
$begingroup$
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/AKX|BKY|^Y(?=B)/A/|s/BKX|AKY|^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 and
3
instead of X
and Y
$_=s/^W(?=(w))|(w)KW/$1.$2^$&/e?redo:y/A//>y/B//
72 bytes
65 bytes
53 bytes
$endgroup$
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/AKX|BKY|^Y(?=B)/A/|s/BKX|AKY|^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 and
3
instead of X
and Y
$_=s/^W(?=(w))|(w)KW/$1.$2^$&/e?redo:y/A//>y/B//
72 bytes
65 bytes
53 bytes
edited Jan 23 at 21:03
answered Jan 22 at 17:56
Nahuel FouilleulNahuel Fouilleul
2,715210
2,715210
1
$begingroup$
Did you forget some command line options?-pe
for instance?
$endgroup$
– Abigail
Jan 23 at 1:57
$begingroup$
from my last understanding they are not counted any more
$endgroup$
– Nahuel Fouilleul
Jan 23 at 6:37
3
$begingroup$
Not counted, but without them, the program doesn't do what's being asked. You can't expect the reader to know which switches your program depends on. Also, the switches are part of language (for the purpose of the contest), so you can have a different winner forperl
,perl -p
, etc.
$endgroup$
– Abigail
Jan 23 at 12:17
1
$begingroup$
Also, you can save 2 bytes by using$_=y/A//<=>y/B//
, outputting -1/0/1 for a win for A, a tie, or a win for B.
$endgroup$
– Abigail
Jan 23 at 12:21
$begingroup$
This does not correctly handleX
andY
at the start of the string. TryXBA
andYAB
.
$endgroup$
– Grimy
Jan 23 at 12:45
|
show 1 more comment
1
$begingroup$
Did you forget some command line options?-pe
for instance?
$endgroup$
– Abigail
Jan 23 at 1:57
$begingroup$
from my last understanding they are not counted any more
$endgroup$
– Nahuel Fouilleul
Jan 23 at 6:37
3
$begingroup$
Not counted, but without them, the program doesn't do what's being asked. You can't expect the reader to know which switches your program depends on. Also, the switches are part of language (for the purpose of the contest), so you can have a different winner forperl
,perl -p
, etc.
$endgroup$
– Abigail
Jan 23 at 12:17
1
$begingroup$
Also, you can save 2 bytes by using$_=y/A//<=>y/B//
, outputting -1/0/1 for a win for A, a tie, or a win for B.
$endgroup$
– Abigail
Jan 23 at 12:21
$begingroup$
This does not correctly handleX
andY
at the start of the string. TryXBA
andYAB
.
$endgroup$
– Grimy
Jan 23 at 12:45
1
1
$begingroup$
Did you forget some command line options?
-pe
for instance?$endgroup$
– Abigail
Jan 23 at 1:57
$begingroup$
Did you forget some command line options?
-pe
for instance?$endgroup$
– Abigail
Jan 23 at 1:57
$begingroup$
from my last understanding they are not counted any more
$endgroup$
– Nahuel Fouilleul
Jan 23 at 6:37
$begingroup$
from my last understanding they are not counted any more
$endgroup$
– Nahuel Fouilleul
Jan 23 at 6:37
3
3
$begingroup$
Not counted, but without them, the program doesn't do what's being asked. You can't expect the reader to know which switches your program depends on. Also, the switches are part of language (for the purpose of the contest), so you can have a different winner for
perl
, perl -p
, etc.$endgroup$
– Abigail
Jan 23 at 12:17
$begingroup$
Not counted, but without them, the program doesn't do what's being asked. You can't expect the reader to know which switches your program depends on. Also, the switches are part of language (for the purpose of the contest), so you can have a different winner for
perl
, perl -p
, etc.$endgroup$
– Abigail
Jan 23 at 12:17
1
1
$begingroup$
Also, you can save 2 bytes by using
$_=y/A//<=>y/B//
, outputting -1/0/1 for a win for A, a tie, or a win for B.$endgroup$
– Abigail
Jan 23 at 12:21
$begingroup$
Also, you can save 2 bytes by using
$_=y/A//<=>y/B//
, outputting -1/0/1 for a win for A, a tie, or a win for B.$endgroup$
– Abigail
Jan 23 at 12:21
$begingroup$
This does not correctly handle
X
and Y
at the start of the string. Try XBA
and YAB
.$endgroup$
– Grimy
Jan 23 at 12:45
$begingroup$
This does not correctly handle
X
and Y
at the start of the string. Try XBA
and YAB
.$endgroup$
– Grimy
Jan 23 at 12:45
|
show 1 more comment
$begingroup$
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!
$endgroup$
add a comment |
$begingroup$
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!
$endgroup$
add a comment |
$begingroup$
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!
$endgroup$
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!
edited Jan 22 at 19:35
answered Jan 22 at 17:29


ArnauldArnauld
78.3k795326
78.3k795326
add a comment |
add a comment |
$begingroup$
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
$endgroup$
add a comment |
$begingroup$
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
$endgroup$
add a comment |
$begingroup$
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
$endgroup$
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
edited Feb 8 at 20:19
answered Jan 22 at 16:54


Kevin CruijssenKevin Cruijssen
40.2k563208
40.2k563208
add a comment |
add a comment |
$begingroup$
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.
$endgroup$
add a comment |
$begingroup$
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.
$endgroup$
add a comment |
$begingroup$
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.
$endgroup$
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.
answered Jan 22 at 22:49
NeilNeil
81.5k745178
81.5k745178
add a comment |
add a comment |
$begingroup$
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
$endgroup$
2
$begingroup$
Your TIO seems to output0
,1
and3
instead of1
and2
?
$endgroup$
– Kevin Cruijssen
Jan 23 at 13:26
1
$begingroup$
@KevinCruijssen But OP allowed truthy vs. falsy as output if i understand correctly. Falsy means 1 won the game, and truthy means 2 won.
$endgroup$
– tsh
Jan 24 at 2:31
$begingroup$
Ah ok, forgot3
is truthy in JS as well. I always think of0
/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.
$endgroup$
– Kevin Cruijssen
Jan 24 at 7:54
$begingroup$
It looks like you could save a byte using bitwise OR, instead of logical OR.
$endgroup$
– Shaggy
Feb 8 at 23:32
$begingroup$
@Shaggy So strange. It works.
$endgroup$
– tsh
Feb 9 at 11:21
add a comment |
$begingroup$
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
$endgroup$
2
$begingroup$
Your TIO seems to output0
,1
and3
instead of1
and2
?
$endgroup$
– Kevin Cruijssen
Jan 23 at 13:26
1
$begingroup$
@KevinCruijssen But OP allowed truthy vs. falsy as output if i understand correctly. Falsy means 1 won the game, and truthy means 2 won.
$endgroup$
– tsh
Jan 24 at 2:31
$begingroup$
Ah ok, forgot3
is truthy in JS as well. I always think of0
/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.
$endgroup$
– Kevin Cruijssen
Jan 24 at 7:54
$begingroup$
It looks like you could save a byte using bitwise OR, instead of logical OR.
$endgroup$
– Shaggy
Feb 8 at 23:32
$begingroup$
@Shaggy So strange. It works.
$endgroup$
– tsh
Feb 9 at 11:21
add a comment |
$begingroup$
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
$endgroup$
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
edited Feb 9 at 11:20
answered Jan 23 at 10:23
tshtsh
9,39511652
9,39511652
2
$begingroup$
Your TIO seems to output0
,1
and3
instead of1
and2
?
$endgroup$
– Kevin Cruijssen
Jan 23 at 13:26
1
$begingroup$
@KevinCruijssen But OP allowed truthy vs. falsy as output if i understand correctly. Falsy means 1 won the game, and truthy means 2 won.
$endgroup$
– tsh
Jan 24 at 2:31
$begingroup$
Ah ok, forgot3
is truthy in JS as well. I always think of0
/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.
$endgroup$
– Kevin Cruijssen
Jan 24 at 7:54
$begingroup$
It looks like you could save a byte using bitwise OR, instead of logical OR.
$endgroup$
– Shaggy
Feb 8 at 23:32
$begingroup$
@Shaggy So strange. It works.
$endgroup$
– tsh
Feb 9 at 11:21
add a comment |
2
$begingroup$
Your TIO seems to output0
,1
and3
instead of1
and2
?
$endgroup$
– Kevin Cruijssen
Jan 23 at 13:26
1
$begingroup$
@KevinCruijssen But OP allowed truthy vs. falsy as output if i understand correctly. Falsy means 1 won the game, and truthy means 2 won.
$endgroup$
– tsh
Jan 24 at 2:31
$begingroup$
Ah ok, forgot3
is truthy in JS as well. I always think of0
/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.
$endgroup$
– Kevin Cruijssen
Jan 24 at 7:54
$begingroup$
It looks like you could save a byte using bitwise OR, instead of logical OR.
$endgroup$
– Shaggy
Feb 8 at 23:32
$begingroup$
@Shaggy So strange. It works.
$endgroup$
– tsh
Feb 9 at 11:21
2
2
$begingroup$
Your TIO seems to output
0
, 1
and 3
instead of 1
and 2
?$endgroup$
– Kevin Cruijssen
Jan 23 at 13:26
$begingroup$
Your TIO seems to output
0
, 1
and 3
instead of 1
and 2
?$endgroup$
– Kevin Cruijssen
Jan 23 at 13:26
1
1
$begingroup$
@KevinCruijssen But OP allowed truthy vs. falsy as output if i understand correctly. Falsy means 1 won the game, and truthy means 2 won.
$endgroup$
– tsh
Jan 24 at 2:31
$begingroup$
@KevinCruijssen But OP allowed truthy vs. falsy as output if i understand correctly. Falsy means 1 won the game, and truthy means 2 won.
$endgroup$
– tsh
Jan 24 at 2:31
$begingroup$
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.$endgroup$
– Kevin Cruijssen
Jan 24 at 7:54
$begingroup$
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.$endgroup$
– Kevin Cruijssen
Jan 24 at 7:54
$begingroup$
It looks like you could save a byte using bitwise OR, instead of logical OR.
$endgroup$
– Shaggy
Feb 8 at 23:32
$begingroup$
It looks like you could save a byte using bitwise OR, instead of logical OR.
$endgroup$
– Shaggy
Feb 8 at 23:32
$begingroup$
@Shaggy So strange. It works.
$endgroup$
– tsh
Feb 9 at 11:21
$begingroup$
@Shaggy So strange. It works.
$endgroup$
– tsh
Feb 9 at 11:21
add a comment |
$begingroup$
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 int
s 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)
$endgroup$
$begingroup$
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.
$endgroup$
– Jonathan Frech
Jan 22 at 22:39
$begingroup$
As a golfing tip,(i, i-1)[i>0]
should be equivalent toi-(i>0)
.
$endgroup$
– Jonathan Frech
Jan 22 at 22:44
$begingroup$
Furthermore, yourif
s could probably becomex[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 thefor
.
$endgroup$
– Jonathan Frech
Jan 22 at 22:47
$begingroup$
@JonathanFrech Thank you very much; I hope I explained the input better
$endgroup$
– user24343
Jan 23 at 16:43
add a comment |
$begingroup$
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 int
s 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)
$endgroup$
$begingroup$
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.
$endgroup$
– Jonathan Frech
Jan 22 at 22:39
$begingroup$
As a golfing tip,(i, i-1)[i>0]
should be equivalent toi-(i>0)
.
$endgroup$
– Jonathan Frech
Jan 22 at 22:44
$begingroup$
Furthermore, yourif
s could probably becomex[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 thefor
.
$endgroup$
– Jonathan Frech
Jan 22 at 22:47
$begingroup$
@JonathanFrech Thank you very much; I hope I explained the input better
$endgroup$
– user24343
Jan 23 at 16:43
add a comment |
$begingroup$
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 int
s 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)
$endgroup$
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 int
s 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)
edited Jan 23 at 21:42


Jonathan Frech
6,37311040
6,37311040
answered Jan 22 at 22:16
user24343user24343
1413
1413
$begingroup$
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.
$endgroup$
– Jonathan Frech
Jan 22 at 22:39
$begingroup$
As a golfing tip,(i, i-1)[i>0]
should be equivalent toi-(i>0)
.
$endgroup$
– Jonathan Frech
Jan 22 at 22:44
$begingroup$
Furthermore, yourif
s could probably becomex[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 thefor
.
$endgroup$
– Jonathan Frech
Jan 22 at 22:47
$begingroup$
@JonathanFrech Thank you very much; I hope I explained the input better
$endgroup$
– user24343
Jan 23 at 16:43
add a comment |
$begingroup$
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.
$endgroup$
– Jonathan Frech
Jan 22 at 22:39
$begingroup$
As a golfing tip,(i, i-1)[i>0]
should be equivalent toi-(i>0)
.
$endgroup$
– Jonathan Frech
Jan 22 at 22:44
$begingroup$
Furthermore, yourif
s could probably becomex[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 thefor
.
$endgroup$
– Jonathan Frech
Jan 22 at 22:47
$begingroup$
@JonathanFrech Thank you very much; I hope I explained the input better
$endgroup$
– user24343
Jan 23 at 16:43
$begingroup$
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.
$endgroup$
– Jonathan Frech
Jan 22 at 22:39
$begingroup$
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.
$endgroup$
– Jonathan Frech
Jan 22 at 22:39
$begingroup$
As a golfing tip,
(i, i-1)[i>0]
should be equivalent to i-(i>0)
.$endgroup$
– Jonathan Frech
Jan 22 at 22:44
$begingroup$
As a golfing tip,
(i, i-1)[i>0]
should be equivalent to i-(i>0)
.$endgroup$
– Jonathan Frech
Jan 22 at 22:44
$begingroup$
Furthermore, your
if
s 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
.$endgroup$
– Jonathan Frech
Jan 22 at 22:47
$begingroup$
Furthermore, your
if
s 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
.$endgroup$
– Jonathan Frech
Jan 22 at 22:47
$begingroup$
@JonathanFrech Thank you very much; I hope I explained the input better
$endgroup$
– user24343
Jan 23 at 16:43
$begingroup$
@JonathanFrech Thank you very much; I hope I explained the input better
$endgroup$
– user24343
Jan 23 at 16:43
add a comment |
$begingroup$
Perl 5, 54 bytes
s/^W(?=(w))|(w)KW/$1^$2^$&/e&&redo;$_=y/A//>y/B//
Try it online!
Uses A
for A
, B
for B
, N
for N
, 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.
$endgroup$
$begingroup$
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
$endgroup$
– Nahuel Fouilleul
Jan 23 at 17:00
add a comment |
$begingroup$
Perl 5, 54 bytes
s/^W(?=(w))|(w)KW/$1^$2^$&/e&&redo;$_=y/A//>y/B//
Try it online!
Uses A
for A
, B
for B
, N
for N
, 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.
$endgroup$
$begingroup$
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
$endgroup$
– Nahuel Fouilleul
Jan 23 at 17:00
add a comment |
$begingroup$
Perl 5, 54 bytes
s/^W(?=(w))|(w)KW/$1^$2^$&/e&&redo;$_=y/A//>y/B//
Try it online!
Uses A
for A
, B
for B
, N
for N
, 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.
$endgroup$
Perl 5, 54 bytes
s/^W(?=(w))|(w)KW/$1^$2^$&/e&&redo;$_=y/A//>y/B//
Try it online!
Uses A
for A
, B
for B
, N
for N
, 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.
answered Jan 23 at 16:18
GrimyGrimy
2,4611019
2,4611019
$begingroup$
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
$endgroup$
– Nahuel Fouilleul
Jan 23 at 17:00
add a comment |
$begingroup$
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
$endgroup$
– Nahuel Fouilleul
Jan 23 at 17:00
$begingroup$
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
$endgroup$
– Nahuel Fouilleul
Jan 23 at 17:00
$begingroup$
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
$endgroup$
– Nahuel Fouilleul
Jan 23 at 17:00
add a comment |
$begingroup$
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).
$endgroup$
add a comment |
$begingroup$
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).
$endgroup$
add a comment |
$begingroup$
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).
$endgroup$
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).
answered Jan 23 at 20:54
M DirrM Dirr
412
412
add a comment |
add a comment |
$begingroup$
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
$endgroup$
$begingroup$
74 bytes by removing whitespace and changing the function to a lambda function
$endgroup$
– Stephen
Feb 8 at 19:21
add a comment |
$begingroup$
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
$endgroup$
$begingroup$
74 bytes by removing whitespace and changing the function to a lambda function
$endgroup$
– Stephen
Feb 8 at 19:21
add a comment |
$begingroup$
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
$endgroup$
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
edited Feb 10 at 15:30
answered Feb 8 at 19:05
ABridgeTooFarABridgeTooFar
136
136
$begingroup$
74 bytes by removing whitespace and changing the function to a lambda function
$endgroup$
– Stephen
Feb 8 at 19:21
add a comment |
$begingroup$
74 bytes by removing whitespace and changing the function to a lambda function
$endgroup$
– Stephen
Feb 8 at 19:21
$begingroup$
74 bytes by removing whitespace and changing the function to a lambda function
$endgroup$
– Stephen
Feb 8 at 19:21
$begingroup$
74 bytes by removing whitespace and changing the function to a lambda function
$endgroup$
– Stephen
Feb 8 at 19:21
add a comment |
If this is an answer to a challenge…
…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.
…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.
More generally…
…Please make sure to answer the question and provide sufficient detail.
…Avoid asking for help, clarification or responding to other answers (use comments instead).
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f178996%2fwho-will-win-the-election%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
$begingroup$
@EriktheOutgolfer ANNY is the same as just A NN. NX and NY become NN.
$endgroup$
– Comrade SparklePony
Jan 22 at 16:46
5
$begingroup$
It might be worth specifying that
none
is the opposite ofnone
, if the behavior forNY
in the comments is correct.$endgroup$
– Kamil Drakari
Jan 22 at 20:48
1
$begingroup$
IMHO there should be testcases beginning with
XA
,XB
,YA
andYB
.$endgroup$
– Neil
Jan 22 at 22:36
1
$begingroup$
May input contains only 1 letters? e.g. "A", "X", "Y", "N".
$endgroup$
– tsh
Jan 23 at 9:56
2
$begingroup$
Does 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?
$endgroup$
– Kevin Cruijssen
Jan 23 at 15:03