g | x | w | all
Bytes Lang Time Link
195SAKO250417T150858ZAcrimori
009APLDyalog Unicode220614T082624ZBubbler
015Regex any flavor220614T062009ZBubbler
032Regex JS flavor220614T060448Zthejonym
022Headass220613T132445Zthejonym
008CJam201110T183651ZJosiahRy
016APL Dyalog Unicode200903T090101ZRazetime
016Keg190825T074154Zlyxal
041C gcc190825T123805ZG. Sliep
033x86 Assembly190828T142555ZFayti170
043Python 3190825T101644ZJoel
033Ruby190828T110339ZG B
038Java JDK190828T102314ZOlivier
062C clang190826T180040Zosuka_
052Python 3190825T071332Znegative
003Stax190826T234814Zrecursiv
046Crystal190826T201435ZRespiteS
00305AB1E190826T125338ZGrimmy
038Python 3190826T021734Zxnor
028Runic Enchantments190825T215723ZDraco18s
029Haskell190825T212350Zankh-mor
003Jelly190825T205835ZDennis
006Jelly190825T065251ZUnrelate
038PHP190825T160554ZNight2
007Jelly190825T122027ZJonathan
011Retina 0.8.2190825T091652ZNeil
012MATL190825T101412ZLuis Men
033Zsh190825T071956ZGammaFun
019sed E190825T090410ZGammaFun
027JavaScript ES6190825T103720ZArnauld
009Brachylog190825T062946ZUnrelate
016Charcoal190825T092148ZNeil
036Haskell190825T085537ZJo King
040Bash190825T081348ZGammaFun
017Perl 6190825T081337ZJo King
056Python 2190825T071906ZChas Bro
111Python190825T071711ZTwilight
033Perl 5 p190825T062007ZXcali

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+\⍤|⌽

Try it on APLgolf!

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)*$

Try it on regex101!

It was pointed out before that the problem can be simplified:

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)*$

Try it online!

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;}

Try It Online!

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}/}

Try it online!

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.

Try it online!

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

"(!;½|':"=['_"|_

Try it online!

Explained:

  1. Takes the implicit input and right shifts the accumulators value to the bottom

  2. 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

C (gcc), 45 41 bytes

f(a,i)char*i;{a=*i?f(a^*i?a:i[1],i+2):a;}

Try it online!

4 bytes shaved off thanks to @ErikF!

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

Try it online!

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:

Therefore, the final accumulator state is:

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:

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.

Ruby, 33 bytes

->a,s{s[/(00|11)*$/]="";s[-1]||a}

Try it online!

Java (JDK), 38 bytes

a->p->p.reduce(a,(s,c)->c<1|c>2?s:c%2)

Try it online!

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);}

Try it online!

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

Try it online!

Fixed inconsistent return type thanks to Chas Brown

Takes input as two strings; the accumulator and the code.

Stax, 3 bytes

F|t

Run and debug it

For each instruction, perform character translation.

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

Try it online!

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.

05AB1E, 3 bytes

ø`:

Try it online!

Zip, dump on the stack, replace.

Python 3, 38 bytes

lambda l:[y for*x,y in l if[y]!=x][-1]

Try it online!

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-=?!

Try it online!

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

Try it online!

Jelly, 3 bytes

y@/

Input is a single list: the accumulator, followed by the pairs.

Try it online!

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

Try it online!

-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ç@ƒ

Try it online!

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.

PHP, 38 bytes

<?=strtr($argn,['00'=>'',11=>''])[-1];

Try it online!

Basically port of Jo King's idea.

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

Try it online! Or verify all test cases

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

Try it online!


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

Try it online!


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.

Try it online! Try it online!

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

Try it online!


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

Try it online!

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

Try it online!

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}ˡ

Try it online!

-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

Try it online!

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

58 bytes Try it online!

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]*$/}

Try it online!

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 2, 56 bytes

f=lambda a,c:f([a,1,0,a][int(c[:2],2)],c[2:])if c else a

Try it online!

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}{

Try it online!

Input is two lines: first line is the command sequence, second is the accumulator.