Stack Exchange Vote Simulator
Gol><> 0.3.11, 13 12 11 bytes
iEh`^=:@)+M
Try it online. Even though this will work fine in the next update, I've listed it as 0.3.11 just in case.
Explanation
i Read char
Eh If EOF, halt and output top of stack as num
`^= Push 1 if char is ^, else 0
:@ Dup and rotate, giving [is^ is^ votecount]
) Compare greater than, pushing 1 or 0 as appropriate
+M Add and subtract 1
Note that the first use of @
pulls a 0 from the bottom of the stack to initialise the vote count for the first iteration
To illustrate with a full table:
Votes before Button Is ^? Compare < Add Subtract 1
1 ^ 1 0 1 0
1 v 0 0 0 -1
0 ^ 1 1 2 1
0 v 0 0 0 -1
-1 ^ 1 1 2 1
-1 v 0 1 1 0
JavaScript (ES7), 47 46 44 43 37 36 bytes
Crossed out 44 is still regular 44 :(
s=>[for(x of s)s=x<"v"?s!=1:!~s-1]|s
Keeps a running total in s
. Uses for of
loop to iterate over each character in the string and updates s
based on current character and previous value.
Edits: Golfed ~s&&-1
to !~s-1
. This expression has to equal 0 if s
equals -1 and -1 otherwise. Saved 6 bytes thanks to @nderscore.
How the expression works:
~s // Bitwise inverse. ~s==0 only if s==-1
! // Logical negate. Casts to boolean. Equivalent to s==-1
-1 // Subtract. Casts to number so true-1 is 1-1 and false-1 is 0-1
x86 machine code, 24 bytes
31 C0 8A 11 84 D2 75 07 C0 E0 02 C0 F8 06 C3 41 38 C2 74 EC 88 D0 EB EA
This is a function using the fastcall calling convention, which takes a string and returns an 8-bit integer.
I tested it with the following C program, which must be compiled for 32-bit mode.
#include <stdio.h>
#include <inttypes.h>
__attribute__ ((aligned (16))) const unsigned char fun[] = {
0x31, //xor eax,eax
0xC0,
0x8A, //mov [ecx],dl
1 | 2<<3,
0x84, //test dl, dl
0xC0 | 2<<3 | 2,
0x75, // jnz
7,
0xC0, //shl al 2
0xC0 | 4<<3,
2,
0xC0, //sar al 6
0xC0 | 7<<3,
6,
0xC3, //ret
0x41, //inc ecx
0x38, //cmp al,dl
0xC0 | 2,
0x74, //je
-20,
0x88, //mov dl,al
0xC0 | 2<<3,
0xEB, //jmp
-22,
};
int main()
{
__fastcall int8_t (*votesimulator)(char*) = fun;
char* s[] = {
"",
"^^",
"^v",
"^",
"v",
"v^",
"vv",
"^^^",
"vvv",
"^^^^",
"vvvv",
"^^^^^",
"vvvvv",
"^^^^^^",
"vvvvvv",
"^^v",
"^v^",
"^vv",
"vv^",
"v^v",
"v^^",
"^vvv^^vv^vv^v^",
"^vvv^^vv^vv^v^^",
"^vvv^^vv^vv^v^^^",
"^vvv^^vv^vv^v^^v",
"^vvv^^vv^vv^v^^vv",
"^vvv^^vv^vv^v^^vvv",
"^vvvvvvvvvvvv",
"^^vvvvvvvvvvvv",
"^^^vvvvvvvvvvvv",
"vvv^^^^^^^^^^^^",
"vv^^^^^^^^^^^^",
"v^^^^^^^^^^^^",
};
for(int i = 0; i < sizeof(s)/sizeof(*s); i++)
printf("%d\n", votesimulator(s[i]));
printf("\n%d\n", sizeof(fun));
for(int i = 0; i < sizeof(fun); i++)
printf("%02X ", fun[i]);
return 0;
}