Before I begin, I should mention that I'm not familiar with meta, and I'm not even sure if this is okay. For the time being, based on the existence of other questions with similar titles, I'm assuming that it is okay to ask for help revising a question on meta, before posting the question on the main site. Please let me know if I'm wrong, and if so, where to go for this specific type of problem.
I recently wrote a question on the main site, and within the span of minutes, accrued four downvotes with no comments explaining why. The only person who commented said that the part about The Witness is irrelevant. This is information that I included because I didn't want to just spam code in a question without talking about some background first.
So I deleted the question. And before I re-post it, I want to make absolutely sure that I do it right so that it won't be negatively received, hence why I am turning to meta for help.
Anyway, below is the full text of the original question (followed by the revision I currently have). The help is much appreciated.
Anybody who's played enough of The Witness probably knows about the puzzle where you have to sit through an hour-long monologue while a moon slowly moves from one side of the screen to the other (full transcript). During this bizarre and long-winded monologue about Video Game Easter Eggs, a mention is made of "Gary's Formula", which involves taking every letter of a string, multiplying its position value by six (A = 6, B = 12, C = 18, etc), and summing them together. This is used as a "proof" that Santa Claus is evil, because
G(Santa Claus) = 666
.So I wrote an implementation of it, and then I wrote a 101-byte golf of it.
main(c,v)char**v;{for(char*a=v[1];*a;a++)c+=6*(*a>64&&*a<91?*a-64:*a>96&&*a<123?*a-96:0);return c-2;}
This is where the question comes in, essentially "why does this happen?":
$ make garygolf && (./garygolf santaclaus; echo $?) # expect: 666 154
If I change the code to include a call to
printf
, it uncovers the strange and interesting bug that is currently blowing my mind.$ cat garygolf.c main(c,v)char**v;{for(char*a=v[1];*a;a++)c+=6*(*a>64&&*a<91?*a-64:*a>96&&*a<123?*a-96:0);printf("%u\n", c-2);return c-2;} $ make garygolf && (./garygolf santaclaus; echo $?) 666 154
I'm not even sure whether it's a bug in the program, a bug in the shell, or if I'm just using the shell wrong. If
$?
somehow wasn't the return value, or something else was modifying the return value just before exit, then adding extra arguments should result in the same return code of 154, but look:$ ./garygolf santaclaus second third; echo $? # result should be off by two 668 156
It's off by two. Where is 154 coming from? I feel like I'm missing something obvious.
Here's some more outputs, showing how deep this unpredictable output goes:
$ dogg(){ ./garygolf "$1"; echo $?; } $ dogg compute 558 46 $ dogg computer 666 154 $ dogg computers 780 12 $ dogg computersa 786 18 $ dogg computersaz 942 174
And as for the first paragraph, here is my second draft:
I recently wrote an implementation of "Gary's Formula". Gary's Formula is described on this 58-minute diatribe about video game easter eggs (you can find the segment by searching for "santa claus"). In brief, the algorithm works as follows: for each letter in string, multiply the letter's positional value by six (A = 6, B = 12, C = 18, etc). The result is the sum of all of the letters' values.
The rest, I haven't changed.