| Bytes | Lang | Time | Link |
|---|---|---|---|
| 195 | SAKO | 250417T150858Z | Acrimori |
| 009 | APLDyalog Unicode | 220614T082624Z | Bubbler |
| 015 | Regex any flavor | 220614T062009Z | Bubbler |
| 032 | Regex JS flavor | 220614T060448Z | thejonym |
| 022 | Headass | 220613T132445Z | thejonym |
| 008 | CJam | 201110T183651Z | JosiahRy |
| 016 | APL Dyalog Unicode | 200903T090101Z | Razetime |
| 016 | Keg | 190825T074154Z | lyxal |
| 041 | C gcc | 190825T123805Z | G. Sliep |
| 033 | x86 Assembly | 190828T142555Z | Fayti170 |
| 043 | Python 3 | 190825T101644Z | Joel |
| 033 | Ruby | 190828T110339Z | G B |
| 038 | Java JDK | 190828T102314Z | Olivier |
| 062 | C clang | 190826T180040Z | osuka_ |
| 052 | Python 3 | 190825T071332Z | negative |
| 003 | Stax | 190826T234814Z | recursiv |
| 046 | Crystal | 190826T201435Z | RespiteS |
| 003 | 05AB1E | 190826T125338Z | Grimmy |
| 038 | Python 3 | 190826T021734Z | xnor |
| 028 | Runic Enchantments | 190825T215723Z | Draco18s |
| 029 | Haskell | 190825T212350Z | ankh-mor |
| 003 | Jelly | 190825T205835Z | Dennis |
| 006 | Jelly | 190825T065251Z | Unrelate |
| 038 | PHP | 190825T160554Z | Night2 |
| 007 | Jelly | 190825T122027Z | Jonathan |
| 011 | Retina 0.8.2 | 190825T091652Z | Neil |
| 012 | MATL | 190825T101412Z | Luis Men |
| 033 | Zsh | 190825T071956Z | GammaFun |
| 019 | sed E | 190825T090410Z | GammaFun |
| 027 | JavaScript ES6 | 190825T103720Z | Arnauld |
| 009 | Brachylog | 190825T062946Z | Unrelate |
| 016 | Charcoal | 190825T092148Z | Neil |
| 036 | Haskell | 190825T085537Z | Jo King |
| 040 | Bash | 190825T081348Z | GammaFun |
| 017 | Perl 6 | 190825T081337Z | Jo King |
| 056 | Python 2 | 190825T071906Z | Chas Bro |
| 111 | Python | 190825T071711Z | Twilight |
| 033 | Perl 5 p | 190825T062007Z | Xcali |
SAKO, 195 bytes
CALKOWITE:X,I,*W,A
CZYTAJ:A
BLOK(2*9):W
CZYTAJWIERSZ:W
*)X=(W(I)-48)×10+W(I+1)-48
GDYX>11:2,INACZEJ1
1)Z=MOD(X,10)
C=1-ABS(A-ENT(X/10))
A=A-C×A+C×Z
POWTORZ:I=0(2)2*9
2)DRUKUJ(0):A
STOP2
KONIEC
Takes input from STDIN in form of:
accumulator value
string of commands without separators
Loops over the string using some simple math to turn A, C, and Z into desired accumulator value.
K) READ ACCUMULATOR
CZYTAJ: A
K) START LOOP, TAKE X
*) X = ...
K) 1 IF X = 01|11, OR 0 IF X = 00|10 VIA MODULO
Z = MOD(ENT(X), 10)
K) 1 WHEN ACTION IS NEEDED, 0 WHEN NOT.
C = 1-ABS(A-ENT(X)/10)
K) SOME SIMPLE MATH
A = A - C×A + C×Z
K) END LOOP
K) PRINT A
DRUKUJ(0): A
Other approach, 122 bytes
CZYTAJ:A
1)CZYTAJ:X
GDYX=2:2,INACZEJ3
3)Z=MOD(X,10)
C=1-ABS(A-ENT(X/10))
A=A-C×A+C×Z
SKOCZDO1
2)DRUKUJ(1,0):A
STOP2
KONIEC
Takes value of accumulator first, and command values each in own line.
Reads the command value multiple times as an integer in an infinite loop.
This approach is a lot shorter, but the last command must have value 2, so is invalid.
Same, but subroutine, 121 bytes
PODPROGRAM:F(A)
1)CZYTAJ:X
GDYX=2:2,INACZEJ3
3)Z=MOD(X,10)
C=1-ABS(A-ENT(X/10))
A=A-C×A+C×Z
SKOCZDO1
2)DRUKUJ(1,0):A
WROC
APL(Dyalog Unicode), 9 bytes SBCS
1∊11+\⍤|⌽
Input format is the same as Razetime's.
1∊11+\⍤|⌽
⌽ Reverse the input list
11 | Modulo 11 (change 11 to 0)
Now the program outputs 1 iff the list starts with 0 0 ... 0 1
+\⍤ Cumulative sum
1∊ Test if it contains a 1
The proof of the statement
a list starts with 0 0 ... 0 1 <=> its cumulative sum contains 1
is left as an exercise to the reader.
Regex (any flavor), 15 bytes
(^|0)1(00|11)*$
It was pointed out before that the problem can be simplified:
00and11are no-ops.01unconditionally sets the value to 1 while10sets to 0.- The output is 1 if and only if the input is 1 and the entire code is no-op or the last non-no-op command is
01.
The regex precisely does the job: (00|11)*$ after discarding no-ops at the end, the last thing found is (^|0)1 the input 1 or a 01 command.
1(00|11)*$ does not work because the 1 may be part of another no-op command 11.
Regex (JS flavor), 32 bytes
^(0(..)*01|1((..)*01)?)(00|11)*$
Matches if output is 1, doesn't if output is 0. Global and multiline flags enabled in link to verify multiple test cases at once, but not necessary for solution. Did I format this submission correctly?
Headass, 22 bytes
U[{N-()]PNE:U(])U[:U;}
U[{N-()]PNE:U(])U[:U;} full program
U[ save initial state of accumulator to storage register
{ : } while
N-() input remains
U(]) ; if input == accumulator state
U[ set accumulator to next input
: else
U discard next input
]P print final state of accumulator
NE go to code block 1
block does not exist, exits with error
There is room for golfing maybe if I can avoid storing anything in the comparison register (i.e. having it remain 0 for the entirety of the program and never using (), but it might not be possible.
CJam, 8 bytes
{{)er}/}
Takes input on the stack in the form of "0" ["00" "01" "11" "11" "01"]. Does string replacement using every command.
APL (Dyalog Unicode), 16 bytes
(+⌷13⍴0 1,9/⊢)/⌽
A tacit function which takes initial accumulator value and the program as a single integer list.
Maps the relevant sums of instruction and accumulator to an array.
Table: (a→accumulator, i→instruction)
a i a+i result
00 0 0 0
01 0 1 1
10 1 11 0
11 1 12 1
All other cases return the same value, so they are assigned to the preexisting value of a.
Explanation
(+⌷13⍴0 1,9/⊢)/⌽ accumulator→a
/⌽ reduce the reversed array using th following function: (reducing happens from the right)
9/⊢) replicate a 9 times
13⍴0 1, concatenate with (0,1) and expand to 13 elements → (0 1 a a a a a a a a a 0 1)
(+⌷ sum first two elements and find element at that index in the array
Solution which accepts a string
{{(13⍴0 1,9/⍺)[⍺+⍵]}/⌽(⍎¨((⍴⍵)⍴1 0)⊂('0',⍵))}
It was pretty ambiguous how the input was supposed to be taken, so I decided I'd leave this one in as well.
This was the original dfn, which was golfed down using Adám and Bubbler's advice.
Keg, -ir, 16 bytes
"(!;½|':"=['_"|_
Explained:
Takes the implicit input and right shifts the accumulators value to the bottom
Repeat the following (length of stack - 1 divided by 2) times
2.1. Shift the accumulator back to the top
2.2. Compare for equality with the first part of the command
2.2.1. If true, replace the accumulator, otherwise pop the replacement
Input is taken as the initial acc value concatenated with the source. E.g.
010011000
- First char is acc value
- Rest is program
x86 Assembly, 33 Bytes
Takes the initial accumulator state in CL (integer 0 or 1) and the address of the commands as a zero-terminated ASCII String in ESI. Leaves the final accumulator state in CL.
Point the call instruction at offset 0x1B (label interpret in the Explanation).
3C 30 74 03 B0 01 C3 30 C0 C3 E8 F1 FF FF FF 38
C8 AC 75 07 E8 E7 FF FF FF 88 C1 AC 84 C0 75 EA
C3
Explanation (Using Intel Syntax):
; function to convert ASCII '1'/'0' into 0 or 1 int values (from AL to AL)
ctob:
CMP AL, 0x30 ; '0'
JE .zero
MOV AL, 1
RET
.zero:
XOR AL, AL
RET
; interpreting function
interp_lp:
CALL ctob ; convert to number
CMP AL, CL ; compare to current accumulator
LODSB ; read the next character of the string
; this doesn't affect any flags and we need to do
; it in both cases anyway
JNE interpret ; if AL != CL (from above, not the new value of AL), skip forward
CALL ctob ; convert AL to number
MOV CL, AL ; store AL in CL
interpret: LODSB ; read the next character of the string
TEST AL, AL ; check if it is a zero byte
JNZ interp_lp ; if not, jump back into the loop
RET
Python 3, 43 bytes
lambda s:re.sub("00|11","",s)[-1]
import re
The function takes a single string as input, where the first character is the initial state and the rest of the string represents the commands. This solution can be easily ported to other languages that have better support for regular expressions.
The difficult part is to prove the solution yields the correct outcome. To see this, we need a deep analysis of the commands. Firstly, we can see the commands have the following properties:
- Property (1): commands
00and11retain the accumulator state. - Property (2): commands
01and10make the accumulator state the same as the second bit regardless of its original state.
Therefore, the final accumulator state is:
- Case 1: If no
01or10command exists, the final state is the same as the initial state. - Case 2: Otherwise, the last bit of the last
10or01command.
Next we will show the solution yields the correct outcome in both cases. We will prove the statement for the final state 0 and the final state of 1 can be proved analogously. If the final state is 0 the input is in either of the following forms:
^0{2k+1}11(11|00)*For Case 1, the input string
smust start with2k+10s, followed by11and00commands. Eliminating00s and11s yields a single0, which is the final state..+10{2k+1}11(11|00)*For Case 2, the input string ends with a
10command, followed by zero or more00and11s. This pattern is equivalent to a1followed by2k+10s, and then zero or more11s and00s. Eliminating00s and11s leaves behind the last one of the2k+10s at the end of the string, which represents the final state.
Based on all the above, after eliminating 00s and 11s simultaneously in one single pass (01001 is a counter-example if 00 is eliminated in one pass and then 11 in another pass) from the input s, the last character is the final state. Hence the correctness of the solution is proved.
Java (JDK), 38 bytes
a->p->p.reduce(a,(s,c)->c<1|c>2?s:c%2)
The inputs are an int and an IntStream of 0, 1, 2 or 3, which correspond to 00, 01, 10, 11 from binary.
C (clang), 68 62 bytes
t(s,e,a)char*s,*e;{for(;s<e;++s)a=*s++-48^a?a:*s-48;puts(&a);}
Takes a pointer to the start of the source string, a pointer to the end of the source string (start + strlen(start)), and the initial accumulator value.
Old version (prints ASCII 48/49 for 0/1):
t(s,e,a)char*s,*e;{for(;s<e;++s)a=*s++-48^a?a:*s-48;putchar(a+48);}
Python 3, 52 bytes
f=lambda a,s:s and f([s[1],a][s[0]==s[1]],s[2:])or a
Fixed inconsistent return type thanks to Chas Brown
Takes input as two strings; the accumulator and the code.
Crystal, 46 bytes
With commands in an Array(Tuple(Int32,Int32)), such as [{0,0}, {0,1}, {0,0}].
def f(s,i);i.map{|c,v|s+=~(s^c)&(s^v)%2};s;end
It's pretty simple to understand in a more readable form:
def f(state, instructions)
instructions.map do |check, value|
state += ~(state ^ check) & (state ^ value) % 2
end
state
end
The function loops through each command, automatically unpacking the tuple values into c and v. It then sets the state by the formula
state = state + NOT(state XOR check) AND (state XOR value) mod 2
which I arrived at mostly by trial and error. Once all commands have been processed, it returns the state value.
Python 3, 38 bytes
lambda l:[y for*x,y in l if[y]!=x][-1]
Based on Joel's solution. Takes input as a list of the initial accumulator value (length-one string) followed by the commands (length-two strings). Finds the last command with two unequal values, and outputs its second character.
To make this fall through to the initial accumulator value when there are no such commands, we make it so that the single-char initial value string passes the test. We do so by checking if a singleton list with the last character is unequal to a list of all preceding characters, which is passed by any length-one string or length-two string with two different characters.
Runic Enchantments, 28 bytes
/~@/i~/i<
/=?/~iR:l}i{l1-=?!
Takes input as a series of space separated bytes (Runic does not understand lists). The first byte is the initial state and every other byte is the program. No validation is performed (i.e. it assumes only valid programs are given as input and it doesn't care what value is used to represent 0 and 1).
Haskell, 29 bytes
Defines an unnamed function on the first line with type (Foldable t, Eq b) => b -> t [b] -> b. For the purposes of this code golf, we can instantiate it as Char -> [String] -> Char where the first argument is the accumulator and the second is a list of string with each string being a single command.
foldl(#)
a#[x,y]|a==x=y|1>0=a
Jelly, 3 bytes
y@/
Input is a single list: the accumulator, followed by the pairs.
How it works
The y atom performs transliteration; [a,b]yc replaces a with b, so it returns b if a=c and c if a≠c.
y@/ folds/reduces the input by y with swapped arguments, performing one transliteration per pair.
Jelly, 8 6 bytes
EÐḟṪṪo
-2 bytes thanks to Nick Kennedy informing me of a rules change. (His proposed golf, EÐḟFȯṪ, seems somewhat more clever but has the same length as my previous solution minus s2.) The input format now takes the commands as a list of two-character strings, but the testing footer translates from the old format for convenience.
Translated from my newer Brachylog solution.
Old version:
Jelly, 13 bytes
ḢẎ⁼⁹a⁸o
s2ç@ƒ
I'm not 100% sure this is correct, but it succeeds on all three test cases. Takes the commands as the left argument and the initial accumulator as the right argument.
Jelly, 7 bytes
fؽḂ⁹;Ṫ
A dyadic Link accepting the program as a list of integers on the left and the initial accumulator on the right which yields an integer.
Try it online! Or see a test-suite
Retina 0.8.2, 18 11 bytes
(.)\1
!`.$
Try it online! Link includes test cases. Takes input concatenated. Saved 6 bytes thanks to @CowsQuack for pointing out that removing all doubled characters and then taking the last remaining character works, although in fact the port of @JoKing's original answer could have been golfed by 3 bytes even without that trick.
MATL, 13 12 bytes
!dh2Ol4$Ys0)
Takes the input as a 2-column matrix where each row is a command, and a number
Zsh, 33 bytes
The character list is passed as arguments, the initial value of the accumulator is passed as stdin.
read a
for x y;a=$[x^a?a:y]
<<<$a
39 bytes: If the commands must be a single string
Input is accumulator commands as arguments.
for x y (${(s::)2})1=$[x^$1?$1:y]
<<<$1
For fun, here's a 50 byte recursive one-liner (TIO):
<<<${${2+`f $[$1^${2[1]}?$1:${2[2]}] ${2:2}`}:-$1}
sed -E, 26 19 bytes
A whopping -7 bytes from @Cowsquack by realizing removing all pairs works as well.
s/(.)\1//g
s/.*\B//
Takes input concatenated together on stdin. Inspired by Jo King's Perl answer.
Strip trailing pairs Remove all pairs, then get last digit.
JavaScript (ES6), 27 bytes
Takes input as (a)(code), where code is a is list of 2-bit integers.
a=>c=>c.map(x=>a^=x==a+1)|a
JavaScript (ES6), 47 40 bytes
Takes input as (a)(code), where code is a string.
a=>c=>c.replace(/../g,x=>a^=x%4==a+1)&&a
How?
All possible cases are summarized below. The only two cases where we need to toggle the accumulator are \$(a=0,x=01_2)\$ and \$(a=1,x=10_2)\$.
a | x (bin) | int(x) % 4 | a + 1 | equal?
----+---------+------------+-------+--------
0 | "00" | 0 % 4 = 0 | 1 | N
1 | "00" | 0 % 4 = 0 | 2 | N
0 | "01" | 1 % 4 = 1 | 1 | Y
1 | "01" | 1 % 4 = 1 | 2 | N
0 | "10" | 10 % 4 = 2 | 1 | N
1 | "10" | 10 % 4 = 2 | 2 | Y
0 | "11" | 11 % 4 = 3 | 1 | N
1 | "11" | 11 % 4 = 3 | 2 | N
Brachylog, 11 9 bytes
tġ₂≠ˢtt|h
Since it's been long enough that I've been able to forget the notion of printing the accumulator after each command, I've formulated a significantly less naïve solution with some inspiration from Jo King's Perl answer.
| The output is
tt the last element of the last element of
t the last element of the input
ġ₂ split into length-2 slices
≠ˢ with equal pairs removed.
| If there is no such element, the input
h 's first element is the output.
Old solution:
Brachylog, 18 16 bytes
ġ₂ᵗc{th~h?tt|h}ˡ
-2 bytes from changing the input format.
Charcoal, 16 bytes
F⪪η²F⁼θ§ι⁰≔§ι¹θθ
Try it online! Link is to verbose version of code. Takes separate arguments. Explanation:
F⪪η²
Split the instructions into pairs of digits and loop over them.
F⁼θ§ι⁰
If the accumulator is equal to the first digit...
≔§ι¹θ
... then assign the second digit to it.
θ
Print the accumulator at the end of the loop.
Haskell, 36 bytes
f(x:y:s)=f s.last.(:[y|x/=y])
f _=id
Takes input as f(string)(char) where the character is the accumulator and the string is the list of commands.
Bash, 58 40 bytes
Add one byte for a full program: change f to $0.
(($1=$2-a?a:$3,1))&&f $1 ${@:4}||echo $1
The ternary will return false when $1 is set to 0, but the ,1 at the end ensures the whole ((expression)) will return true, except a syntax error.
When all the arguments are consumed, a syntax error happens and the recursion ends.
Perl 6, 17 bytes
{m/.)>[(.)$0]*$/}
Takes advantage of "You can merge these two inputs into one input if you like" by taking input as the accumulator value concatenated with the commands e.g. 1,[00,11] is 10011. If this isn't okay, than it's only 5 extra bytes to take it as f(accumulator, commands). Returns a match object that can be coerced to a string.
Explanation:
{ } # Anonymous code block
m/ / # Find the first match from the input
.)> # Capture a number
[ ]* # Followed by any number of
(.)$0 # Pairs of identical characters
$ # Ending the string
Basically this works because the 00 and 11 commands do literally nothing, while the 01 and 10 commands just set the accumulator to the second digit of the command. If there are no commands, then it takes the initial value of the accumulator instead.
Python, 111 bytes
def f(a,b):
c=a
for i in range(0,len(b)-1,2):
c=(not b[i])*(c or b[i] or b[i+1]) or c*b[i]*b[i+1]
return c
Ungolfed. EDIT: AHHH Someone beat me to it!
Perl 5 -p, 37 33 bytes
$\=<>;s/(.)(.)/$\=$2if$\==$1/ge}{
Input is two lines: first line is the command sequence, second is the accumulator.