Cubix, 24 19 bytes
)uO)ABq-!wpUp)W.@;;
Note
- Actually counts how many of the same characters are at the end of the input, so this works for really big integers and really long strings as well (as long as the amount of same characters at the end is smaller than the maximum precision of JavaScript (around 15 digits in base-10).
- Input goes in the input field, output is printed to the output field
Try it here
Explanation
First, let's expand the cube
) u
O )
A B q - ! w p U
p ) W . @ ; ; .
. .
. .
The steps in the execution can be split up in three phases:
- Parse input
- Compare characters
- Print result
Phase 1: Input
The first two characters that are executed are A
and B
. A
reads all input and pushes it as character codes to the stack. Note that this is done in reverse, the first character ends up on top of the stack, the last character almost at the bottom. At the very bottom, -1
(EOF
) is placed, which will be used as a counter for the amount of consecutive characters at the end of the string. Since we need the top of the stack to contain the last two characters, we reverse the stack, before entering the loop. Note that the top part of the stack now looks like: ..., C[n-1], C[n], -1
.
The IP's place on the cube is where the E
is, and it's pointing right. All instructions that have not yet been executed, were replaced by no-ops (full stops).
. .
. .
A B E . . . . .
. . . . . . . .
. .
. .
Phase 2: Character comparison
The stack is ..., C[a-1], C[a], counter
, where counter
is the counter to increment when the two characters to check (C[a]
and C[a-1]
) are equal. The IP first enters this loop at the S
character, moving right. The E
character is the position where the IP will end up (pointing right) when C[a]
and C[a-1]
do not have the same value, which means that subtracting C[a]
from C[a-1]
does not yield 0
, in which case the instruction following the !
will be skipped (which is a w
).
. .
. .
. S q - ! w E .
p ) W . . ; ; .
. .
. .
Here are the instructions that are executed during a full loop:
q-!;;p) # Explanation
q # Push counter to the bottom of the stack
# Stack (counter, ..., C[a-1], C[a])
- # Subtract C[a] from C[a-1], which is 0 if both are equal
# Stack (counter, ..., C[a-1], C[a], C[a-1]-C[a])
! # Leave the loop if C[a-1]-C[a] does not equal 0
;; # Remove result of subtraction and C[a] from stack
# Stack (counter, ..., C[a-1])
p # Move the bottom of the stack to the top
# Stack (..., C[a-1], counter)
) # Increment the counter
# Stack (..., C[a-1], counter + 1)
And then it loops around.
Phase 3: Print result
Since we left the loop early, the stack looks like this: counter, ..., C[a-1]-C[a]
. It's easy to print the counter, but we have to increment the counter once because we didn't do it in the last iteration of the loop, and once more because we started counting at -1
instead of 0
. The path on the cube looks like this, starting at S
, pointing right. The two no-ops that are executed by the IP are replaced by arrows that point in the direction of the IP.
) u
O )
. B . . . S p U
. ) . . @ . . .
> >
. .
The instructions are executed in the following order. Note that the B)
instructions at the end change the stack, but don't affect the program, since we are about to terminate it, and we do not use the stack anymore.
p))OB)@ # Explanation
p # Pull the counter to the top
# Stack: (..., counter)
)) # Add two
# Stack: (..., counter + 2)
O # Output as number
B) # Reverse the stack and increment the top
@ # End the program
Alea iacta est.
1Are we allowed to take input as string? – Dead Possum – 2017-03-28T09:11:51.967
6@DeadPossum I would assume that's allowed, since you get a string anyway if you read the input from STDIN, command-line argument or file (which are all admissible input methods). – Martin Ender – 2017-03-28T09:19:50.253
1Can we assume that the input will be greater than 0? – Martin Ender – 2017-03-28T10:01:33.423
1@MartinEnder Yes – sagiksp – 2017-03-28T10:05:07.743
2Upvote for the dubs game! Check'em! – ZombieChowder – 2017-03-28T12:31:07.083
@sagiksp That goes against our default. Is that really your intent?
– Mego – 2017-03-28T13:47:14.787@Mego Nevermind you can use it all – sagiksp – 2017-03-28T16:15:23.557
Could someone make a solution that actually uses 4chan and some URL-related feature? – ckjbgames – 2017-03-28T20:31:42.607
@ckjbgames What would it be, the user gets a url and has to regex the number out first? I'm thinking maybe a comment finder by ID would work (Looks through all threads with sequential id to find a specific id comment, because you can't index by comment in the 4chan api), but for this challeange it just won't work. – sagiksp – 2017-03-28T20:39:35.060
Disappointed: The question ID on SE does not have dubs. – Num Lock – 2017-03-30T09:42:12.147
@NumLock That would have been so perfect :D – sagiksp – 2017-03-30T19:30:58.450