25
1
It's a normal truth machine but instead of taking input, it uses the first character of the program. Thus, internal.
The 0 and 1 are plain characters, i.e. ASCII code 0x30 and 0x31 respectively.
Example: 0abcd
prints 0 and halts, and 1abcd
prints 1 infinitely. Then your submission is abcd
, whose score is 4 bytes. The 0 or 1 (the first character of the program) is not counted towards your score.
Of course, you're not allowed to look inside the file itself. Like a quine.
Leaderboard
Here is a Stack Snippet to generate both a regular leaderboard and an overview of winners by language.
/* Configuration */
var QUESTION_ID = 198288; // Obtain this from the url
// It will be like https://XYZ.stackexchange.com/questions/QUESTION_ID/... on any question page
var ANSWER_FILTER = "!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe";
var COMMENT_FILTER = "!)Q2B_A2kjfAiU78X(md6BoYk";
var OVERRIDE_USER = 48934; // This should be the user ID of the challenge author.
/* App */
var answers = [], answers_hash, answer_ids, answer_page = 1, more_answers = true, comment_page;
function answersUrl(index) {
return "https://api.stackexchange.com/2.2/questions/" + QUESTION_ID + "/answers?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + ANSWER_FILTER;
}
function commentUrl(index, answers) {
return "https://api.stackexchange.com/2.2/answers/" + answers.join(';') + "/comments?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + COMMENT_FILTER;
}
function getAnswers() {
jQuery.ajax({
url: answersUrl(answer_page++),
method: "get",
dataType: "jsonp",
crossDomain: true,
success: function (data) {
answers.push.apply(answers, data.items);
answers_hash = [];
answer_ids = [];
data.items.forEach(function(a) {
a.comments = [];
var id = +a.share_link.match(/\d+/);
answer_ids.push(id);
answers_hash[id] = a;
});
if (!data.has_more) more_answers = false;
comment_page = 1;
getComments();
}
});
}
function getComments() {
jQuery.ajax({
url: commentUrl(comment_page++, answer_ids),
method: "get",
dataType: "jsonp",
crossDomain: true,
success: function (data) {
data.items.forEach(function(c) {
if (c.owner.user_id === OVERRIDE_USER)
answers_hash[c.post_id].comments.push(c);
});
if (data.has_more) getComments();
else if (more_answers) getAnswers();
else process();
}
});
}
getAnswers();
var SCORE_REG = /<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/;
var OVERRIDE_REG = /^Override\s*header:\s*/i;
function getAuthorName(a) {
return a.owner.display_name;
}
function process() {
var valid = [];
answers.forEach(function(a) {
var body = a.body;
a.comments.forEach(function(c) {
if(OVERRIDE_REG.test(c.body))
body = '<h1>' + c.body.replace(OVERRIDE_REG, '') + '</h1>';
});
var match = body.match(SCORE_REG);
if (match)
valid.push({
user: getAuthorName(a),
size: +match[2],
language: match[1],
link: a.share_link,
});
});
valid.sort(function (a, b) {
var aB = a.size,
bB = b.size;
return aB - bB
});
var languages = {};
var place = 1;
var lastSize = null;
var lastPlace = 1;
valid.forEach(function (a) {
if (a.size != lastSize)
lastPlace = place;
lastSize = a.size;
++place;
var answer = jQuery("#answer-template").html();
answer = answer.replace("{{PLACE}}", lastPlace + ".")
.replace("{{NAME}}", a.user)
.replace("{{LANGUAGE}}", a.language)
.replace("{{SIZE}}", a.size)
.replace("{{LINK}}", a.link);
answer = jQuery(answer);
jQuery("#answers").append(answer);
var lang = a.language;
if (/<a/.test(lang)) lang = jQuery(lang).text();
languages[lang] = languages[lang] || {lang: a.language, user: a.user, size: a.size, link: a.link};
});
var langs = [];
for (var lang in languages)
if (languages.hasOwnProperty(lang))
langs.push(languages[lang]);
langs.sort(function (a, b) {
if (a.lang > b.lang) return 1;
if (a.lang < b.lang) return -1;
return 0;
});
for (var i = 0; i < langs.length; ++i)
{
var language = jQuery("#language-template").html();
var lang = langs[i];
language = language.replace("{{LANGUAGE}}", lang.lang)
.replace("{{NAME}}", lang.user)
.replace("{{SIZE}}", lang.size)
.replace("{{LINK}}", lang.link);
language = jQuery(language);
jQuery("#languages").append(language);
}
}
body { text-align: left !important}
#answer-list {
padding: 10px;
width: 290px;
float: left;
}
#language-list {
padding: 10px;
width: 290px;
float: left;
}
table thead {
font-weight: bold;
}
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="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b">
<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>
<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>
<table style="display: none">
<tbody id="answer-template">
<tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr>
</tbody>
</table>
<table style="display: none">
<tbody id="language-template">
<tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr>
</tbody>
</table>
3Wait, why did you post it so fast from the Sandbox? – None – 2020-01-21T14:01:01.407
5So, to make sure I understand, this requires two programs in the same language that differ only in the first character being either 0 or 1? – histocrat – 2020-01-21T14:02:57.483
Yes, and it acts like an input to the normal truth machine. – Want – 2020-01-21T14:05:18.137
Does the input bit count as part of the program? – None – 2020-01-21T14:06:40.163
Well, when I had it in mind, I thought of including it, but now, I guess not. – Want – 2020-01-21T14:08:55.790
Must output be STDOUT or is STDERR or exit code also fine? – Jitse – 2020-01-21T14:36:26.230
5
Quasi-duplicate of Implement a Truth-Machine. The added restriction on input doesn't make it distinct in an interesting way.
– Grimmy – 2020-01-21T15:39:57.953I agree with the suggested dupe. Let's dupe-hammer this if Grimmy's comment reaches 4 votes (assuming it's not closed as dupe first by standard votes) – Luis Mendo – 2020-01-21T16:09:21.173
1
@Grimy, My code would change quite a bit though, shaving ten bytes off, and my answer here wouldn't work.
– ouflak – 2020-01-21T16:13:06.1906
@LuisMendo, But I think one major qualification for whether a question here is a duplicate is whether the answers here would answer the question for the proposed duplicate (even with trivial changes). But I don't think that is the case. Maybe for a couple of languages. But not for many of these. The Lua answer there would not work here, and in fact, it's impossible to answer this challenge here in Lua. That certainly has to be taken into consideration.
– ouflak – 2020-01-21T16:33:01.4002Although I agree with Grimmy and something else could have been chosen as output for
1
/0
respectively, I don't really see it as a dupe. Uninteresting since we already have the other challenge: definitely; but dupe: not really. This challenge isn't even possible in Java or Whitespace for example (a.f.a.i.k.), and although the core part is indeed the same, getting around that leading0
/1
will be tricky in some languages. EDIT: Ah, @ouflak already mentioned everything I said I now see.. – Kevin Cruijssen – 2020-01-21T16:34:50.470@ouflak, Kevin: Ok, not a dupe then – Luis Mendo – 2020-01-21T17:11:10.807
2Could those who VTC as unclear please state what they require? I have voted to re-open, since the post is clear. – Jonathan Allan – 2020-01-21T17:16:44.297
Does this first byte need to literally be
0x00
(ASCII NUL) or0x01
(ASCII SOH), or the ASCII character0
or1
(0x30
/0x31
)? (I assume the latter since the former would invalidate some of the existing answers) – 640KB – 2020-01-21T17:20:39.027@640KB "it uses the first character of the program."; "The 0 or 1 at the start does not count to the bytes" – Jonathan Allan – 2020-01-21T17:22:54.223
@JonathanAllan I'm just trying to clarify that the spec doesn't require that the first byte of the program be an actual
0x0
value (null byte)when (representing the0
input) vs. a human-readable representation of a "0" char. For example, if you were tohexdump
a Python source file would the first byte be0x0
(non-printable) or0x30
. character would seem to imply that it is indeed the ASCII value. Again, just clarifying rules. – 640KB – 2020-01-21T17:25:22.28010@JonathanAllan The challenge is only clear to me when taking into account the comments. If the challenge is edited with most of those changes added in I will gladly reopen. – FryAmTheEggman – 2020-01-21T17:36:21.050
This would have been a bit more interesting if there was a third thing the program had to do when not prefixed with a 0/1 – Jo King – 2020-01-23T06:32:35.850
Oh that would be interesting, but what should it do? Print 1 only once? – Want – 2020-01-23T13:19:20.323