Talk interpreter
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:
- Property (1): commands
00
and11
retain the accumulator state. - Property (2): commands
01
and10
make the accumulator state the same as the second bit regardless of its original state.
Therefore, the final accumulator state is:
- Case 1: If no
01
or10
command exists, the final state is the same as the initial state. - Case 2: Otherwise, the last bit of the last
10
or01
command.
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
s
must start with2k+1
0s, followed by11
and00
commands. Eliminating00
s and11
s yields a single0
, which is the final state..+10{2k+1}11(11|00)*
For Case 2, the input string ends with a
10
command, followed by zero or more00
and11
s. This pattern is equivalent to a1
followed by2k+1
0s, and then zero or more11
s and00
s. Eliminating00
s and11
s leaves behind the last one of the2k+1
0s at the end of the string, which represents the final state.
Based on all the above, after eliminating 00
s and 11
s 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.
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]y
c 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.
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.