"Talk" is a baroquified accumulator-based language that is created in response to Dennis's quote on talk.tryitonline.net.
Waiting for someone to create an esolang called talk.
. The "Talk" language has 4 commands:
00
If the accumulator is 0, set the accumulator to 0.01
If the accumulator is 0, set the accumulator to 1.10
If the accumulator is 1, set the accumulator to 0.11
If the accumulator is 1, set the accumulator to 1.
Input:
The input can be taken via any acceptable input method by our standard I/O rules.
There are two inputs, the initial accumulator value and the program. You can merge these two inputs into one input or split your input into valid commands (e.g. taking them as a list; e.g.
[00, 01, 00]
) if you like.
Output:
- On the end of a command execution, the accumulator is outputted implicitly.
Rules:
- The input can be a single string or character list.
- As this is code-golf, the shortest answer, in bytes, wins.
- We take digits or strings/characters.
Test cases:
0 0001111101 -> 1
0 000100 -> 1
0 11001000 -> 0
Leaderboards
Here is a Stack Snippet to generate both a regular leaderboard and an overview of winners by language.
To make sure that your answer shows up, please start your answer with a headline, using the following Markdown template:
# Language Name, N bytes
where N
is the size of your submission. If you improve your score, you can keep old scores in the headline, by striking them through. For instance:
# Ruby, <s>104</s> <s>101</s> 96 bytes
If there you want to include multiple numbers in your header (e.g. because your score is the sum of two files or you want to list interpreter flag penalties separately), make sure that the actual score is the last number in the header:
# Perl, 43 + 2 (-p flag) = 45 bytes
You can also make the language name a link which will then show up in the leaderboard snippet:
# [><>](http://esolangs.org/wiki/Fish), 121 bytes
var QUESTION_ID=190819;
var OVERRIDE_USER=8478;
var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;function answersUrl(d){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+d+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(d,e){return"https://api.stackexchange.com/2.2/answers/"+e.join(";")+"/comments?page="+d+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(d){answers.push.apply(answers,d.items),answers_hash=[],answer_ids=[],d.items.forEach(function(e){e.comments=[];var f=+e.share_link.match(/\d+/);answer_ids.push(f),answers_hash[f]=e}),d.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(d){d.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),d.has_more?getComments():more_answers?getAnswers():process()}})}getAnswers();var SCORE_REG=function(){var d=String.raw`h\d`,e=String.raw`\-?\d+\.?\d*`,f=String.raw`[^\n<>]*`,g=String.raw`<s>${f}</s>|<strike>${f}</strike>|<del>${f}</del>`,h=String.raw`[^\n\d<>]*`,j=String.raw`<[^\n<>]+>`;return new RegExp(String.raw`<${d}>`+String.raw`\s*([^\n,]*[^\s,]),.*?`+String.raw`(${e})`+String.raw`(?=`+String.raw`${h}`+String.raw`(?:(?:${g}|${j})${h})*`+String.raw`</${d}>`+String.raw`)`)}(),OVERRIDE_REG=/^Override\s*header:\s*/i;function getAuthorName(d){return d.owner.display_name}function process(){var d=[];answers.forEach(function(n){var o=n.body;n.comments.forEach(function(q){OVERRIDE_REG.test(q.body)&&(o="<h1>"+q.body.replace(OVERRIDE_REG,"")+"</h1>")});var p=o.match(SCORE_REG);p&&d.push({user:getAuthorName(n),size:+p[2],language:p[1],link:n.share_link})}),d.sort(function(n,o){var p=n.size,q=o.size;return p-q});var e={},f=1,g=null,h=1;d.forEach(function(n){n.size!=g&&(h=f),g=n.size,++f;var o=jQuery("#answer-template").html();o=o.replace("{{PLACE}}",h+".").replace("{{NAME}}",n.user).replace("{{LANGUAGE}}",n.language).replace("{{SIZE}}",n.size).replace("{{LINK}}",n.link),o=jQuery(o),jQuery("#answers").append(o);var p=n.language;p=jQuery("<i>"+n.language+"</i>").text().toLowerCase(),e[p]=e[p]||{lang:n.language,user:n.user,size:n.size,link:n.link,uniq:p}});var j=[];for(var k in e)e.hasOwnProperty(k)&&j.push(e[k]);j.sort(function(n,o){return n.uniq>o.uniq?1:n.uniq<o.uniq?-1:0});for(var l=0;l<j.length;++l){var m=jQuery("#language-template").html(),k=j[l];m=m.replace("{{LANGUAGE}}",k.lang).replace("{{NAME}}",k.user).replace("{{SIZE}}",k.size).replace("{{LINK}}",k.link),m=jQuery(m),jQuery("#languages").append(m)}}
body{text-align:left!important}#answer-list{padding:10px;float:left}#language-list{padding:10px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="https://cdn.sstatic.net/Sites/codegolf/primary.css?v=f52df912b654"> <div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td><a href="{{LINK}}">{{SIZE}}</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td><a href="{{LINK}}">{{SIZE}}</a></td></tr></tbody> </table>
38 Answers 38
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]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.
-
17\$\begingroup\$ This is the only Jelly answer that I have seen so far that only uses ASCII characters. \$\endgroup\$user85052– user850522019年08月26日 00:38:19 +00:00Commented Aug 26, 2019 at 0:38
-
3\$\begingroup\$ There were a few. Look ma, no Unicode! \$\endgroup\$Dennis– Dennis2019年08月28日 12:47:19 +00:00Commented Aug 28, 2019 at 12:47
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
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.
-
\$\begingroup\$ Welcome to PPCG! Excellent answer, and a nice formal proof to go with it! \$\endgroup\$GammaFunction– GammaFunction2019年08月25日 10:21:06 +00:00Commented Aug 25, 2019 at 10:21
-
3\$\begingroup\$ Thanks. I feel that people may doubt such a simple solution yields the correct outcome at the first glance. So there is a need to provide a proof for that. \$\endgroup\$Joel– Joel2019年08月25日 10:24:36 +00:00Commented Aug 25, 2019 at 10:24
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.
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ドル}
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.
-
\$\begingroup\$ Oh no, that was fast. \$\endgroup\$TwilightSparkle– TwilightSparkle2019年08月25日 07:19:31 +00:00Commented Aug 25, 2019 at 7:19
-
1\$\begingroup\$ Nice but it has this potential problem -
f(1,'11')==f(1,'01')
isFalse
; it sometimes returns anint
and sometimes astr
. So maybe specify it takes acc input as a string? \$\endgroup\$Chas Brown– Chas Brown2019年08月25日 07:42:06 +00:00Commented Aug 25, 2019 at 7:42 -
\$\begingroup\$ @ChasBrown Good call, that's much simpler than what I was thinking about. \$\endgroup\$negative seven– negative seven2019年08月25日 07:48:38 +00:00Commented Aug 25, 2019 at 7:48
-
Brachylog, (削除) 11 (削除ここまで) 9 bytes
tġ2≠stt|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
ġ2 split into length-2 slices
≠s 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
ġ2tc{th~h?tt|h}l
-2 bytes from changing the input format.
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
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!
-
1\$\begingroup\$ The last line can be simply
s/.*\B//
, but anyways changing the approach slightly gives an even shorter 19 bytes Try it online! \$\endgroup\$user41805– user418052019年08月25日 10:54:32 +00:00Commented Aug 25, 2019 at 10:54 -
1\$\begingroup\$ Huh, I didn't think that
s/(.)1円//g
would work, since it could remove the end of one pair and the start of the next, but it still works out. Excellent! \$\endgroup\$GammaFunction– GammaFunction2019年08月25日 12:35:05 +00:00Commented Aug 25, 2019 at 12:35 -
\$\begingroup\$ @GammaFunction
s/(.)1円//g
is equivalent tos/00|11//g
as shown in my solution. \$\endgroup\$Joel– Joel2019年08月25日 15:26:05 +00:00Commented Aug 25, 2019 at 15:26
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.
-
\$\begingroup\$ 12 bytes Try it online! \$\endgroup\$user41805– user418052019年08月25日 10:52:33 +00:00Commented Aug 25, 2019 at 10:52
-
\$\begingroup\$ @Cowsquack D'oh, once you think about separating the stages, that's a 2-byte saving already, then another byte saved because you can use
!`.$
, and then another 4 bytes because you don't have to limit to trailing pairs... \$\endgroup\$Neil– Neil2019年08月25日 15:12:05 +00:00Commented Aug 25, 2019 at 15:12
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
-
1\$\begingroup\$ Same bytecount if you define it using prefix notation. I can't believe I wrote almost the exact same answer at the same time, even including the type signature explanation... \$\endgroup\$cole– cole2019年08月25日 21:48:48 +00:00Commented Aug 25, 2019 at 21:48
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.
Perl 5 -p
, (削除) 37 (削除ここまで) 33 bytes
$\=<>;s/(.)(.)/$\=2ドルif$\==1ドル/ge}{
Input is two lines: first line is the command sequence, second is the accumulator.
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
ḢẎ=9a8o
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.
-
1\$\begingroup\$ The input is allowed to be split into a list, so you could have
EÐḟFȯṪ
with the input as e.g.[[0,0],[0,1],[1,1],[1,1],[0,1]]
. \$\endgroup\$Nick Kennedy– Nick Kennedy2019年08月25日 19:30:22 +00:00Commented Aug 25, 2019 at 19:30 -
\$\begingroup\$ Wow, the changes to the spec really were quite large... \$\endgroup\$Unrelated String– Unrelated String2019年08月25日 19:42:01 +00:00Commented Aug 25, 2019 at 19:42
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.
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?
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!
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.
Keg, -ir
, 16 bytes
"(!;1⁄2|':"=['_"|_
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
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.
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.
Regex (any flavor), 15 bytes
(^|0)1(00|11)*$
It was pointed out before that the problem can be simplified:
00
and11
are no-ops.01
unconditionally sets the value to 1 while10
sets 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
.
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.
Charcoal, 16 bytes
F⪪η2F=θ§ι0≔§ι1θθ
Try it online! Link is to verbose version of code. Takes separate arguments. Explanation:
F⪪η2
Split the instructions into pairs of digits and loop over them.
F=θ§ι0
If the accumulator is equal to the first digit...
≔§ι1θ
... then assign the second digit to it.
θ
Print the accumulator at the end of the loop.
MATL, (削除) 13 (削除ここまで) 12 bytes
!dh2Ol4$Ys0)
Takes the input as a 2-column matrix where each row is a command, and a number
Jelly, 7 bytes
fØ1⁄2Ḃ9;Ṫ
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
-
\$\begingroup\$ I am removing input-mapping because @GammaFunction recommended me to do this. \$\endgroup\$user85052– user850522019年08月25日 13:30:09 +00:00Commented Aug 25, 2019 at 13:30
-
\$\begingroup\$ @A_ ah OK cant delete on mobile so will have to address this later \$\endgroup\$Jonathan Allan– Jonathan Allan2019年08月25日 13:35:56 +00:00Commented Aug 25, 2019 at 13:35
-
\$\begingroup\$ @A_ fixed up to work with 0,1,2,3 the unmapped version of the instructions as digits is that OK? \$\endgroup\$Jonathan Allan– Jonathan Allan2019年08月25日 16:01:22 +00:00Commented Aug 25, 2019 at 16:01
-
1\$\begingroup\$ Yes, that's OK. \$\endgroup\$user85052– user850522019年08月25日 21:51:07 +00:00Commented Aug 25, 2019 at 21:51
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
).
lang.tryitonline.net
(now it'stio.run/#lang
). So a language called Talk would cause confusion with the URL for the then-recently created chatroom, which istalk.tryitonline.net
\$\endgroup\$