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 if 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 if 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" has an asterisk to their left, 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.
X*X Person to left is voting "none" so votes "none."
*A Votes for 1
A*Y Since person on left is voting for 1, votes for 2.
*A Votes for 1
*N Does not vote
Final poll: 1|2|None (cannot win)
2|1|3
1 has the most votes and is not "None", 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 (for output), etc.)
Input -> Output
"AAAA" -> 1
"BBBB" -> 2
"BBAXY" -> 2
"BAXYBNXBAYXBN" -> 2
"XXAYAN" -> 1
"AAAABXXXX" -> 2
"AXNXXXXAYB" -> 1
"NANNY" -> 1
"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$
add a comment |
$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 if 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 if 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" has an asterisk to their left, 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.
X*X Person to left is voting "none" so votes "none."
*A Votes for 1
A*Y Since person on left is voting for 1, votes for 2.
*A Votes for 1
*N Does not vote
Final poll: 1|2|None (cannot win)
2|1|3
1 has the most votes and is not "None", 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 (for output), etc.)
Input -> Output
"AAAA" -> 1
"BBBB" -> 2
"BBAXY" -> 2
"BAXYBNXBAYXBN" -> 2
"XXAYAN" -> 1
"AAAABXXXX" -> 2
"AXNXXXXAYB" -> 1
"NANNY" -> 1
"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$
$begingroup$
CanNX
orNY
appear?
$endgroup$
– Erik the Outgolfer
51 mins ago
$begingroup$
@EriktheOutgolfer Yes, but not alone. (See last valid test case)
$endgroup$
– Comrade SparklePony
50 mins ago
$begingroup$
So,ANNY
is the same asAY
? Like, do we just removeN
s?
$endgroup$
– Erik the Outgolfer
49 mins ago
$begingroup$
@EriktheOutgolfer ANNY is the same as just A NN. NX and NY become NN.
$endgroup$
– Comrade SparklePony
48 mins ago
add a comment |
$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 if 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 if 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" has an asterisk to their left, 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.
X*X Person to left is voting "none" so votes "none."
*A Votes for 1
A*Y Since person on left is voting for 1, votes for 2.
*A Votes for 1
*N Does not vote
Final poll: 1|2|None (cannot win)
2|1|3
1 has the most votes and is not "None", 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 (for output), etc.)
Input -> Output
"AAAA" -> 1
"BBBB" -> 2
"BBAXY" -> 2
"BAXYBNXBAYXBN" -> 2
"XXAYAN" -> 1
"AAAABXXXX" -> 2
"AXNXXXXAYB" -> 1
"NANNY" -> 1
"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 if 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 if 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" has an asterisk to their left, 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.
X*X Person to left is voting "none" so votes "none."
*A Votes for 1
A*Y Since person on left is voting for 1, votes for 2.
*A Votes for 1
*N Does not vote
Final poll: 1|2|None (cannot win)
2|1|3
1 has the most votes and is not "None", 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 (for output), etc.)
Input -> Output
"AAAA" -> 1
"BBBB" -> 2
"BBAXY" -> 2
"BAXYBNXBAYXBN" -> 2
"XXAYAN" -> 1
"AAAABXXXX" -> 2
"AXNXXXXAYB" -> 1
"NANNY" -> 1
"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
asked 1 hour ago
Comrade SparklePonyComrade SparklePony
3,28111352
3,28111352
$begingroup$
CanNX
orNY
appear?
$endgroup$
– Erik the Outgolfer
51 mins ago
$begingroup$
@EriktheOutgolfer Yes, but not alone. (See last valid test case)
$endgroup$
– Comrade SparklePony
50 mins ago
$begingroup$
So,ANNY
is the same asAY
? Like, do we just removeN
s?
$endgroup$
– Erik the Outgolfer
49 mins ago
$begingroup$
@EriktheOutgolfer ANNY is the same as just A NN. NX and NY become NN.
$endgroup$
– Comrade SparklePony
48 mins ago
add a comment |
$begingroup$
CanNX
orNY
appear?
$endgroup$
– Erik the Outgolfer
51 mins ago
$begingroup$
@EriktheOutgolfer Yes, but not alone. (See last valid test case)
$endgroup$
– Comrade SparklePony
50 mins ago
$begingroup$
So,ANNY
is the same asAY
? Like, do we just removeN
s?
$endgroup$
– Erik the Outgolfer
49 mins ago
$begingroup$
@EriktheOutgolfer ANNY is the same as just A NN. NX and NY become NN.
$endgroup$
– Comrade SparklePony
48 mins ago
$begingroup$
Can
NX
or NY
appear?$endgroup$
– Erik the Outgolfer
51 mins ago
$begingroup$
Can
NX
or NY
appear?$endgroup$
– Erik the Outgolfer
51 mins ago
$begingroup$
@EriktheOutgolfer Yes, but not alone. (See last valid test case)
$endgroup$
– Comrade SparklePony
50 mins ago
$begingroup$
@EriktheOutgolfer Yes, but not alone. (See last valid test case)
$endgroup$
– Comrade SparklePony
50 mins ago
$begingroup$
So,
ANNY
is the same as AY
? Like, do we just remove N
s?$endgroup$
– Erik the Outgolfer
49 mins ago
$begingroup$
So,
ANNY
is the same as AY
? Like, do we just remove N
s?$endgroup$
– Erik the Outgolfer
49 mins ago
$begingroup$
@EriktheOutgolfer ANNY is the same as just A NN. NX and NY become NN.
$endgroup$
– Comrade SparklePony
48 mins ago
$begingroup$
@EriktheOutgolfer ANNY is the same as just A NN. NX and NY become NN.
$endgroup$
– Comrade SparklePony
48 mins ago
add a comment |
3 Answers
3
active
oldest
votes
$begingroup$
05AB1E, 34 bytes
gFÐNè©2@iNx1.S-èDÄ2‹*®3Qi±>}Nǝ]O.±
Uses an integer-array as input with A=-1, B=1, N=0, X=2, Y=3
and outputs -1
if A wins and 1
if B wins.
Can probably be golfed a bit more..
Try it online or verify all test cases.
Explanation:
g # Take the length of the (implicit) input-list
F # Loop `N` in the range [0, length):
Ð # Triplicate the list (which is the implicit input-list in the first iteration)
Nè # Get the `N`'th item of the list
© # Store it in the register (without popping)
2@i # If it's larger or equal to 2 (so either 2 or 3):
Nx # Push `N` and `N` doubled both to the stack
1.S # Compare the double integer with 1 (-1 if N*2<1; 0 if N*2==1; 1 if N*2>1)
# (So if it's the first iteration (N=0), this will be -1, otherwise it will be 1)
- # Subtract that from the index
è # And index it into the list to get the next or previous item
DÄ2‹ # Check if that value is -1, 0, or 1 (abs(i) < 2) (1 if truthy; 0 if falsey)
* # And multiply that with the indexed next/previous item
®3Qi } # If the `N`'th value (from the register) was a 3:
±> # Bitwise-NOT + 1 it (1 becomes -1; 0 remains 0; 1 becomes -1)
Nǝ # And replace the `N`'th value with this
] # Close both the if-statement and loop
O # Sum the modified list (which now only contains -1, 0, or 1)
.± # And take its signum (-1 if negative; 0 if 0; 1 if positive)
$endgroup$
add a comment |
$begingroup$
Java 8, 153 bytes
a->{int l=a.length,t,i=-1;for(;++i<l;a[i]=a[i]>4?t<3?t^3:3:a[i]>3?t:a[i])t=a[i>0?i-1:i<l-1?i+1:i];t=i=0;for(int n:a){if(n<2)i++;if(n==2)t++;}return i>t;}
Uses an integer array as input with A=1, B=2, N=3, X=4, Y=5
and outputs a boolean true
if A wins and false
if B wins.
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
i=-1;for(;++i<l // Loop `i` in the range [0, l):
; // After every iteration:
a[i]= // Change the `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 instead
: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
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
t=i=0; // Reset both `t` and `i` to 0
for(int n:a){ // Loop over the values of the now modified array:
if(n<2) // If the current value is a 1:
i++; // Increase `i` by 1
if(n==2) // If the current value is a 2:
t++;} // Increase `t` by 1
return i>t;} // Return whether `i` is larger than `t`
$endgroup$
add a comment |
$begingroup$
JavaScript (ES6), 78 bytes
Takes input as an array of integers with: $0$ = N, $1$ = A, $2$ = B, $4$ = X, $8$ = Y.
Returns $0$ if the first candidate wins or $1$ if the 2nd candidate wins.
a=>a.map((x,i)=>++v[p=x?x&3||(x&8&&3)^(p&3||a[i+1]&3):0],v=[,p=0,0])|v[1]<v[2]
Try it online!
$endgroup$
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
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
05AB1E, 34 bytes
gFÐNè©2@iNx1.S-èDÄ2‹*®3Qi±>}Nǝ]O.±
Uses an integer-array as input with A=-1, B=1, N=0, X=2, Y=3
and outputs -1
if A wins and 1
if B wins.
Can probably be golfed a bit more..
Try it online or verify all test cases.
Explanation:
g # Take the length of the (implicit) input-list
F # Loop `N` in the range [0, length):
Ð # Triplicate the list (which is the implicit input-list in the first iteration)
Nè # Get the `N`'th item of the list
© # Store it in the register (without popping)
2@i # If it's larger or equal to 2 (so either 2 or 3):
Nx # Push `N` and `N` doubled both to the stack
1.S # Compare the double integer with 1 (-1 if N*2<1; 0 if N*2==1; 1 if N*2>1)
# (So if it's the first iteration (N=0), this will be -1, otherwise it will be 1)
- # Subtract that from the index
è # And index it into the list to get the next or previous item
DÄ2‹ # Check if that value is -1, 0, or 1 (abs(i) < 2) (1 if truthy; 0 if falsey)
* # And multiply that with the indexed next/previous item
®3Qi } # If the `N`'th value (from the register) was a 3:
±> # Bitwise-NOT + 1 it (1 becomes -1; 0 remains 0; 1 becomes -1)
Nǝ # And replace the `N`'th value with this
] # Close both the if-statement and loop
O # Sum the modified list (which now only contains -1, 0, or 1)
.± # And take its signum (-1 if negative; 0 if 0; 1 if positive)
$endgroup$
add a comment |
$begingroup$
05AB1E, 34 bytes
gFÐNè©2@iNx1.S-èDÄ2‹*®3Qi±>}Nǝ]O.±
Uses an integer-array as input with A=-1, B=1, N=0, X=2, Y=3
and outputs -1
if A wins and 1
if B wins.
Can probably be golfed a bit more..
Try it online or verify all test cases.
Explanation:
g # Take the length of the (implicit) input-list
F # Loop `N` in the range [0, length):
Ð # Triplicate the list (which is the implicit input-list in the first iteration)
Nè # Get the `N`'th item of the list
© # Store it in the register (without popping)
2@i # If it's larger or equal to 2 (so either 2 or 3):
Nx # Push `N` and `N` doubled both to the stack
1.S # Compare the double integer with 1 (-1 if N*2<1; 0 if N*2==1; 1 if N*2>1)
# (So if it's the first iteration (N=0), this will be -1, otherwise it will be 1)
- # Subtract that from the index
è # And index it into the list to get the next or previous item
DÄ2‹ # Check if that value is -1, 0, or 1 (abs(i) < 2) (1 if truthy; 0 if falsey)
* # And multiply that with the indexed next/previous item
®3Qi } # If the `N`'th value (from the register) was a 3:
±> # Bitwise-NOT + 1 it (1 becomes -1; 0 remains 0; 1 becomes -1)
Nǝ # And replace the `N`'th value with this
] # Close both the if-statement and loop
O # Sum the modified list (which now only contains -1, 0, or 1)
.± # And take its signum (-1 if negative; 0 if 0; 1 if positive)
$endgroup$
add a comment |
$begingroup$
05AB1E, 34 bytes
gFÐNè©2@iNx1.S-èDÄ2‹*®3Qi±>}Nǝ]O.±
Uses an integer-array as input with A=-1, B=1, N=0, X=2, Y=3
and outputs -1
if A wins and 1
if B wins.
Can probably be golfed a bit more..
Try it online or verify all test cases.
Explanation:
g # Take the length of the (implicit) input-list
F # Loop `N` in the range [0, length):
Ð # Triplicate the list (which is the implicit input-list in the first iteration)
Nè # Get the `N`'th item of the list
© # Store it in the register (without popping)
2@i # If it's larger or equal to 2 (so either 2 or 3):
Nx # Push `N` and `N` doubled both to the stack
1.S # Compare the double integer with 1 (-1 if N*2<1; 0 if N*2==1; 1 if N*2>1)
# (So if it's the first iteration (N=0), this will be -1, otherwise it will be 1)
- # Subtract that from the index
è # And index it into the list to get the next or previous item
DÄ2‹ # Check if that value is -1, 0, or 1 (abs(i) < 2) (1 if truthy; 0 if falsey)
* # And multiply that with the indexed next/previous item
®3Qi } # If the `N`'th value (from the register) was a 3:
±> # Bitwise-NOT + 1 it (1 becomes -1; 0 remains 0; 1 becomes -1)
Nǝ # And replace the `N`'th value with this
] # Close both the if-statement and loop
O # Sum the modified list (which now only contains -1, 0, or 1)
.± # And take its signum (-1 if negative; 0 if 0; 1 if positive)
$endgroup$
05AB1E, 34 bytes
gFÐNè©2@iNx1.S-èDÄ2‹*®3Qi±>}Nǝ]O.±
Uses an integer-array as input with A=-1, B=1, N=0, X=2, Y=3
and outputs -1
if A wins and 1
if B wins.
Can probably be golfed a bit more..
Try it online or verify all test cases.
Explanation:
g # Take the length of the (implicit) input-list
F # Loop `N` in the range [0, length):
Ð # Triplicate the list (which is the implicit input-list in the first iteration)
Nè # Get the `N`'th item of the list
© # Store it in the register (without popping)
2@i # If it's larger or equal to 2 (so either 2 or 3):
Nx # Push `N` and `N` doubled both to the stack
1.S # Compare the double integer with 1 (-1 if N*2<1; 0 if N*2==1; 1 if N*2>1)
# (So if it's the first iteration (N=0), this will be -1, otherwise it will be 1)
- # Subtract that from the index
è # And index it into the list to get the next or previous item
DÄ2‹ # Check if that value is -1, 0, or 1 (abs(i) < 2) (1 if truthy; 0 if falsey)
* # And multiply that with the indexed next/previous item
®3Qi } # If the `N`'th value (from the register) was a 3:
±> # Bitwise-NOT + 1 it (1 becomes -1; 0 remains 0; 1 becomes -1)
Nǝ # And replace the `N`'th value with this
] # Close both the if-statement and loop
O # Sum the modified list (which now only contains -1, 0, or 1)
.± # And take its signum (-1 if negative; 0 if 0; 1 if positive)
answered 40 mins ago
Kevin CruijssenKevin Cruijssen
36.6k555192
36.6k555192
add a comment |
add a comment |
$begingroup$
Java 8, 153 bytes
a->{int l=a.length,t,i=-1;for(;++i<l;a[i]=a[i]>4?t<3?t^3:3:a[i]>3?t:a[i])t=a[i>0?i-1:i<l-1?i+1:i];t=i=0;for(int n:a){if(n<2)i++;if(n==2)t++;}return i>t;}
Uses an integer array as input with A=1, B=2, N=3, X=4, Y=5
and outputs a boolean true
if A wins and false
if B wins.
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
i=-1;for(;++i<l // Loop `i` in the range [0, l):
; // After every iteration:
a[i]= // Change the `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 instead
: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
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
t=i=0; // Reset both `t` and `i` to 0
for(int n:a){ // Loop over the values of the now modified array:
if(n<2) // If the current value is a 1:
i++; // Increase `i` by 1
if(n==2) // If the current value is a 2:
t++;} // Increase `t` by 1
return i>t;} // Return whether `i` is larger than `t`
$endgroup$
add a comment |
$begingroup$
Java 8, 153 bytes
a->{int l=a.length,t,i=-1;for(;++i<l;a[i]=a[i]>4?t<3?t^3:3:a[i]>3?t:a[i])t=a[i>0?i-1:i<l-1?i+1:i];t=i=0;for(int n:a){if(n<2)i++;if(n==2)t++;}return i>t;}
Uses an integer array as input with A=1, B=2, N=3, X=4, Y=5
and outputs a boolean true
if A wins and false
if B wins.
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
i=-1;for(;++i<l // Loop `i` in the range [0, l):
; // After every iteration:
a[i]= // Change the `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 instead
: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
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
t=i=0; // Reset both `t` and `i` to 0
for(int n:a){ // Loop over the values of the now modified array:
if(n<2) // If the current value is a 1:
i++; // Increase `i` by 1
if(n==2) // If the current value is a 2:
t++;} // Increase `t` by 1
return i>t;} // Return whether `i` is larger than `t`
$endgroup$
add a comment |
$begingroup$
Java 8, 153 bytes
a->{int l=a.length,t,i=-1;for(;++i<l;a[i]=a[i]>4?t<3?t^3:3:a[i]>3?t:a[i])t=a[i>0?i-1:i<l-1?i+1:i];t=i=0;for(int n:a){if(n<2)i++;if(n==2)t++;}return i>t;}
Uses an integer array as input with A=1, B=2, N=3, X=4, Y=5
and outputs a boolean true
if A wins and false
if B wins.
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
i=-1;for(;++i<l // Loop `i` in the range [0, l):
; // After every iteration:
a[i]= // Change the `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 instead
: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
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
t=i=0; // Reset both `t` and `i` to 0
for(int n:a){ // Loop over the values of the now modified array:
if(n<2) // If the current value is a 1:
i++; // Increase `i` by 1
if(n==2) // If the current value is a 2:
t++;} // Increase `t` by 1
return i>t;} // Return whether `i` is larger than `t`
$endgroup$
Java 8, 153 bytes
a->{int l=a.length,t,i=-1;for(;++i<l;a[i]=a[i]>4?t<3?t^3:3:a[i]>3?t:a[i])t=a[i>0?i-1:i<l-1?i+1:i];t=i=0;for(int n:a){if(n<2)i++;if(n==2)t++;}return i>t;}
Uses an integer array as input with A=1, B=2, N=3, X=4, Y=5
and outputs a boolean true
if A wins and false
if B wins.
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
i=-1;for(;++i<l // Loop `i` in the range [0, l):
; // After every iteration:
a[i]= // Change the `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 instead
: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
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
t=i=0; // Reset both `t` and `i` to 0
for(int n:a){ // Loop over the values of the now modified array:
if(n<2) // If the current value is a 1:
i++; // Increase `i` by 1
if(n==2) // If the current value is a 2:
t++;} // Increase `t` by 1
return i>t;} // Return whether `i` is larger than `t`
edited 23 mins ago
answered 51 mins ago
Kevin CruijssenKevin Cruijssen
36.6k555192
36.6k555192
add a comment |
add a comment |
$begingroup$
JavaScript (ES6), 78 bytes
Takes input as an array of integers with: $0$ = N, $1$ = A, $2$ = B, $4$ = X, $8$ = Y.
Returns $0$ if the first candidate wins or $1$ if the 2nd candidate wins.
a=>a.map((x,i)=>++v[p=x?x&3||(x&8&&3)^(p&3||a[i+1]&3):0],v=[,p=0,0])|v[1]<v[2]
Try it online!
$endgroup$
add a comment |
$begingroup$
JavaScript (ES6), 78 bytes
Takes input as an array of integers with: $0$ = N, $1$ = A, $2$ = B, $4$ = X, $8$ = Y.
Returns $0$ if the first candidate wins or $1$ if the 2nd candidate wins.
a=>a.map((x,i)=>++v[p=x?x&3||(x&8&&3)^(p&3||a[i+1]&3):0],v=[,p=0,0])|v[1]<v[2]
Try it online!
$endgroup$
add a comment |
$begingroup$
JavaScript (ES6), 78 bytes
Takes input as an array of integers with: $0$ = N, $1$ = A, $2$ = B, $4$ = X, $8$ = Y.
Returns $0$ if the first candidate wins or $1$ if the 2nd candidate wins.
a=>a.map((x,i)=>++v[p=x?x&3||(x&8&&3)^(p&3||a[i+1]&3):0],v=[,p=0,0])|v[1]<v[2]
Try it online!
$endgroup$
JavaScript (ES6), 78 bytes
Takes input as an array of integers with: $0$ = N, $1$ = A, $2$ = B, $4$ = X, $8$ = Y.
Returns $0$ if the first candidate wins or $1$ if the 2nd candidate wins.
a=>a.map((x,i)=>++v[p=x?x&3||(x&8&&3)^(p&3||a[i+1]&3):0],v=[,p=0,0])|v[1]<v[2]
Try it online!
answered 5 mins ago
ArnauldArnauld
73.7k689309
73.7k689309
add a comment |
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
$begingroup$
Can
NX
orNY
appear?$endgroup$
– Erik the Outgolfer
51 mins ago
$begingroup$
@EriktheOutgolfer Yes, but not alone. (See last valid test case)
$endgroup$
– Comrade SparklePony
50 mins ago
$begingroup$
So,
ANNY
is the same asAY
? Like, do we just removeN
s?$endgroup$
– Erik the Outgolfer
49 mins ago
$begingroup$
@EriktheOutgolfer ANNY is the same as just A NN. NX and NY become NN.
$endgroup$
– Comrade SparklePony
48 mins ago