14
\$\begingroup\$

Challenge

The challenge is simple: print a snake.
You will get the length of the snake as input.
A snake of length 2 looks like this:

==(:)-

A snake of length 7 looks like this:

=======(:)-

In other words, the length of a snake is how many equal signs are before the head.

Usage

Let's say I made a C++ implementation and compiled it to ./getsnake.
I could run it like so:

$ ./getsnake 10
==========(:)-

Clarifications

  • Standard loopholes are disallowed.
  • You can get input and output in any acceptable way.
  • You can assume all inputs given are positive integers.
  • You may write a function instead of a regular program.
\$\endgroup\$
14
  • 8
    \$\begingroup\$ It's kinda useless to leave a challenge in the Sandbox for only half an hour. You did get some feedback, but it's usually best to leave it in the Sandbox for 24-72 hours. (Also, you should either flesh out the "Origins" part or remove it.) \$\endgroup\$ Jul 31, 2016 at 20:15
  • \$\begingroup\$ Is a function enough or do you want a full program? \$\endgroup\$
    – betseg
    Jul 31, 2016 at 20:17
  • \$\begingroup\$ @betseg function is enough \$\endgroup\$ Jul 31, 2016 at 20:17
  • 4
    \$\begingroup\$ I'm afraid all My Squiggly Lamp answers are effortlessly portable to this. \$\endgroup\$
    – manatwork
    Aug 1, 2016 at 12:49
  • 3
    \$\begingroup\$ Negative integers should cause the snake to swallow itself. \$\endgroup\$ Aug 2, 2016 at 12:35

89 Answers 89

19
\$\begingroup\$

Hexagony, 33 bytes

Not gonna win, but still cool. Might be able to get golfed more.

Try it online!

Golfed:

61}?.$)@$j;(3<./;.}l/.400;5\j;.'\

Formatted:

    6 1 } ?
   . $ ) @ $
  j ; ( 3 < .
 / ; . } l / .
  4 0 0 ; 5 \
   j ; . ' \
    . . . .

Colored (Made using Timwi's Hexagony Colorer)

u found da secret snek! =======(:)-

Explanation:

Loop, print out "=" until the counter reaches 0.

    6 1 } ?
   . $ . . $
  . . ( . . .
 / . . } . . .
  . . . ; . .
   . . . ' .
    . . . .

Print "("

    . . . .
   . . . . .
  . . . . . .
 / ; . . . . .
  4 . . . . .
   j . . . .
    . . . .

Print ":"

    . . . .
   . . . . .
  . ; ( 3 < .
 . . . } l / .
  . . . . . .
   . . . . .
    . . . .

Print ")"

    . . . .
   . . . . .
  j . . . . .
 . . . . . . .
  . . . ; 5 \
   . . . . \
    . . . .

The above values were generated using a simple python script. However, I kind of ran out of room for the "-". So, I had to resort to more advanced tricks.

When the program prints out ")", the value of the cell isn't 41, it's 1065. Hexagony just mods the value when printing. As it turns out, (1065*1000+4)%256=44, just one away from 45, the ascii value for "-". Then, I just increment, print, and insert a @ somewhere after printing.

    . . . .
   . $ ) @ $
  j . . 3 . .
 / . . } . . .
  4 0 0 . 5 \
   . ; . . \
    . . . .
\$\endgroup\$
5
  • \$\begingroup\$ How can it be called a programming language when .j4/; is ... Print "("??? Which part of .j4/; is the print and which part is the string to print? \$\endgroup\$
    – Fogmeister
    Aug 1, 2016 at 23:36
  • 2
    \$\begingroup\$ @Fogmeister [Part 1/2] In Hexagony, any alphabetical character sets the value of the current memory cell to the ascii value of that character. "j" has an ascii value of 106, so that is what the memory is set to. Also, and numerical characters multiply the value of the current cell by 10, then add themselves to it (this allows for easy construction of numbers, like the 61 at the beginning. \$\endgroup\$
    – Blue
    Aug 2, 2016 at 0:23
  • 3
    \$\begingroup\$ [Part 2/2] So, j4 sets the memory cell to 1064. When the IP reaches the "/", it gets reflected into ";", which prints out the ascii character corresponding to the current memory cell % 256. 1064%256=40, the ascii value for "(". I hope this answers your question. \$\endgroup\$
    – Blue
    Aug 2, 2016 at 0:25
  • \$\begingroup\$ thanks. Yeah it does. I guess this is more of a language to use as a way of testing your brain. Rather than a way to write programmes then? Interesting. \$\endgroup\$
    – Fogmeister
    Aug 2, 2016 at 0:27
  • \$\begingroup\$ @Fogmeister there are many esoteric programming languages that are more brain twisting exercises than useful for doing real work, although they CAN do real work if you try hard enough. \$\endgroup\$
    – Sparr
    Aug 9, 2016 at 1:53
12
\$\begingroup\$

Cinnamon Gum, 7 bytes

0000000: 7043 dc95 6d4f ce                        pC..mO.

Try it online.

Would have been 6 bytes with the old p syntax :/

Explanation

Decompresses to p~=~(:)-, the p stage then simply reads input and repeats the = n times.

\$\endgroup\$
2
  • \$\begingroup\$ Can you link to language spec for this? \$\endgroup\$ Aug 1, 2016 at 5:07
  • \$\begingroup\$ @RobertFraser I don't have a formal specification or any documentation currently, but you can check out the source at the GitHub repo: github.com/quartata/cinnamon-gum \$\endgroup\$ Aug 1, 2016 at 12:52
12
\$\begingroup\$

Brian & Chuck, 31 bytes

,{-?>}-):(=?
#}<<.{?_<.<.<.<.<.

Input in the form of a byte value, so e.g. input ! gives a snake of length 33.

Try it online!

It's been a while...

Explanation

A quick Brian & Chuck primer:

  • The first line of the program is Brian, the second is Chuck.
  • Brian and Chuck are two Brainfuck-like instances. The main catch is that Chuck's program is Brian's tape and vice versa. The tape heads/instruction pointers start on the first cell of each tape and execution starts on Brian.
  • As for the commands, there are a few differences. Only Brian can use , (input) and only Chuck can use . (output). In addition to < and > there are { and } which move the tape head up to the next zero cell (or in the case of { to the left end of the tape if there is no zero cell on the way). Instead of [...], the only control flow is ? which switches control to the other instance if the current cell is non-zero. The first executed instruction on the other cell is the one after the condition. And finally, _ is just an alias for null-bytes, for convenience.

Now the code. Brian starts with this:

,{-?

This reads the input into Chuck's first cell, then moves the tape head to the left with { (does nothing right now) and decrements the input with - before switching control for Chuck if the value is still non-zero. This begins the main loop. Chuck then runs this bit:

}<<.{?

This moves the tape head on Brian to the very end, moves two cells left onto the = and prints it before the tape head all the way to the left and switching control back to Brian. This is how loops generally work in B&C.

Once the input has been reduced to zero, the ? on Brian's tape will do nothing. Then Brian executes this part:

>}-):(=?

The ):(= are no-ops, so the actual code is just >}-?. We move off the zero cell with >, move up to _ with }, decrement it to make it non-zero and switch to Chuck with ?. Then the last bit on Chuck is run:

<.<.<.<.<.

This simply prints the five characters in front of Chuck, i.e. =(:)-. Note that we need to print another = since the main loop is only executed N-1 times for input N.

\$\endgroup\$
1
  • 10
    \$\begingroup\$ Congrats on 100k \$\endgroup\$
    – Insane
    Aug 1, 2016 at 10:47
10
\$\begingroup\$

V, 8 bytes

Àé=A(:)-

Try it online!

V uses the "Latin1" encoding.

Explanation:

À        "Arg1 times:
 é=      "Insert an '='
   A(:)- "Append the head
\$\endgroup\$
3
  • \$\begingroup\$ I count 10 bytes \$\endgroup\$
    – Zwei
    Aug 1, 2016 at 6:32
  • 2
    \$\begingroup\$ @zwei see my edit. \$\endgroup\$
    – DJMcMayhem
    Aug 1, 2016 at 6:37
  • \$\begingroup\$ Got it! That's better. \$\endgroup\$
    – Zwei
    Aug 1, 2016 at 6:39
6
\$\begingroup\$

Retina, 10 bytes

.+
$*=(:)-

Try it online!

This is a simple regex substitution.

It matches .+ which matches the whole input, and then substitutes it with $*=(;)-.

The $* is a feature unique in Retina: it is the character-repetition special operator.

For example, 5$*x would become xxxxx.

In the case that the previous argument is absent, the whole match is used as the default argument.

\$\endgroup\$
5
\$\begingroup\$

Python, 21 bytes

lambda n:"="*n+"(:)-"

Ideone it!

\$\endgroup\$
6
  • \$\begingroup\$ You can cut 3 bytes by doing: "="*input()+"(:)-" \$\endgroup\$
    – gowrath
    Aug 29, 2016 at 16:57
  • \$\begingroup\$ @gowrath Then it won't be printing anything \$\endgroup\$
    – Leaky Nun
    Aug 29, 2016 at 18:31
  • \$\begingroup\$ It'll output it if you're in the interpreter no? Op said you can get output in any way. \$\endgroup\$
    – gowrath
    Aug 29, 2016 at 18:59
  • \$\begingroup\$ @gowrath You can't assume that we're in REPL \$\endgroup\$
    – Leaky Nun
    Aug 29, 2016 at 19:00
  • \$\begingroup\$ Is that a general rule for code golfing? New here :) \$\endgroup\$
    – gowrath
    Aug 29, 2016 at 19:02
5
\$\begingroup\$

Haskell, 25 bytes

f n=('='<$[1..n])++"(:)-"

'='<$[1..n] is equivalent to replicate n '='.

\$\endgroup\$
5
\$\begingroup\$

Java 8, 52 bytes

n->new String(new char[n]).replace("\0","=")+"(:)-";

Test suite. (Compile > Execute)

Credits.

teh traditional way, 61 54 53 bytes

7 bytes thanks to Kevin Cruijssen.

1 byte thanks to Dom Hastings.

n->{String s="";for(;n-->0;)s+="=";return s+"(:)-";};
\$\endgroup\$
4
  • \$\begingroup\$ +1 Oh, and for the traditional one: for(int i=0;i<n;i++) can be golfed to for(;n>0;n--), since you don't need the input for anything else than the for-loop anyway. \$\endgroup\$ Aug 1, 2016 at 7:00
  • \$\begingroup\$ @KevinCruijssen Thanks, edited \$\endgroup\$
    – Leaky Nun
    Aug 1, 2016 at 7:04
  • \$\begingroup\$ Testing via the link you provided it looks like you can change the for loop to: for(;n-->0;) to save another byte! \$\endgroup\$ Aug 1, 2016 at 15:58
  • \$\begingroup\$ @DomHastings edited with thanks \$\endgroup\$
    – Leaky Nun
    Aug 1, 2016 at 16:18
4
\$\begingroup\$

Jelly, 9 bytes

”=x;“(:)-

Try it online!

”=x;“(:)-
”=          '='
  x         repeat (argument) that many times
   ;        append
    “(:)-   "(:)-"
\$\endgroup\$
4
\$\begingroup\$

C, 38

f(n){for(;n--;printf(n?"=":"=(:)-"));}

Try it on ideone.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Not shorter, but cooler: f(n){~-printf(n?"=":"=(:)-")||f(~-n);}. \$\endgroup\$
    – orlp
    Aug 1, 2016 at 0:45
  • \$\begingroup\$ f(n){for(;n;printf(n--?"=":"(:)-"));} for -1. \$\endgroup\$ Aug 1, 2016 at 17:59
  • \$\begingroup\$ @EʀɪᴋᴛʜᴇGᴏʟғᴇʀ - That doesn't output the head. \$\endgroup\$
    – owacoder
    Aug 1, 2016 at 18:20
  • \$\begingroup\$ @owacoder Well, C is not my primary language... \$\endgroup\$ Aug 1, 2016 at 18:33
  • 1
    \$\begingroup\$ @EʀɪᴋᴛʜᴇGᴏʟғᴇʀ And that's why you shouldn't post untested golfing suggestions (especially if it would have taken you 5 seconds to test your suggestion with the link provided in the answer). \$\endgroup\$ Aug 1, 2016 at 18:40
4
\$\begingroup\$

05AB1E, 10 9 bytes

'=×"ÿ(:)-

Explanation

'=         # push equal-sign
  ×        # repeat input nr of times
   "ÿ(:)-  # interpolate snake body with head as a string
           # implicitly display

Try it online

1 byte saved thanks to Adnan.

\$\endgroup\$
2
  • \$\begingroup\$ '=×"ÿ(:)- for 9 bytes :). This uses string interpolation. \$\endgroup\$
    – Adnan
    Jul 31, 2016 at 20:30
  • 1
    \$\begingroup\$ @Adnan: Aaah, so that's what ÿ does :) \$\endgroup\$
    – Emigna
    Jul 31, 2016 at 20:31
4
\$\begingroup\$

Javascript, 23 bytes

n=>"=".repeat(n)+"(:)-"
\$\endgroup\$
6
  • 1
    \$\begingroup\$ I was just about to post exactly that code! Beat me to it! Have an upvote \$\endgroup\$
    – bren
    Jul 31, 2016 at 22:18
  • \$\begingroup\$ Today I learned about arrow functions. Thank you. Didn't know that was a thing \$\endgroup\$ Aug 2, 2016 at 21:55
  • \$\begingroup\$ How do you call that instance of the function? I just tried n(3) and received a Uncaught ReferenceError: n is not defined error... \$\endgroup\$ Aug 9, 2016 at 0:46
  • \$\begingroup\$ @WallyWest - try x=n=>"=".repeat(n)+"(:)-"; x(7); \$\endgroup\$
    – eithed
    Aug 9, 2016 at 6:11
  • \$\begingroup\$ @eithedog Then shouldn't the answer have the initial x= and changed to 25 chars? \$\endgroup\$ Aug 9, 2016 at 6:36
4
\$\begingroup\$

C#, 28 bytes

n=>new string('=',n)+"(:)-";
\$\endgroup\$
8
  • \$\begingroup\$ im always interested by the lambda snippets. how can you even run this? \$\endgroup\$ Aug 1, 2016 at 19:54
  • \$\begingroup\$ @downrep_nation it takes in an int and automatically returns the string because it is only one statement \$\endgroup\$ Aug 2, 2016 at 7:58
  • \$\begingroup\$ The answer should be a full program or a function, not just a snippet. So this answer seems incomplete. \$\endgroup\$
    – raznagul
    Aug 2, 2016 at 14:57
  • \$\begingroup\$ @raznagul This is an anonymous function therefore it is complete \$\endgroup\$ Aug 2, 2016 at 14:59
  • \$\begingroup\$ @TheLethalCoder: Maybe try online link would help to understand it. (csharppad.com) \$\endgroup\$
    – raznagul
    Aug 2, 2016 at 15:12
4
\$\begingroup\$

Python, 24 bytes.

print"="*input()+"(:)-"

input() gets input from user

*, when used on strings and an integer, creates a new string, which is made of joined copies of the original. For example: "hello "*3 outputs hello hello hello.

By multiplying = and input(), you get a string of = the length that the user specifies.

Using + joins two strings, in this case, our body "=…=" with our head, "(:)-" to make the snake.

print outputs the result.

\$\endgroup\$
0
3
\$\begingroup\$

GolfScript, 11 10 bytes

~"="*"(:)-"

Multiplies "=" by input, and adds head.

-1 thanks to Leaky Nun

\$\endgroup\$
0
3
\$\begingroup\$

Mathematica, 21 20 bytes

"="~Table~#<>"(:)-"&

Anonymous function. Takes a number n as input, and returns a snake of length n as output. "="~Table~# generates a list {"=", "=", ..., "="} of length n, and <>"(:)-" concatenates the list's elements and appends "(:)-" to the resulting string.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ golfable by one more byte: "="~Table~#<>"(:)-"& \$\endgroup\$
    – LLlAMnYP
    Aug 2, 2016 at 7:43
3
\$\begingroup\$

R, 32 27 bytes

This solution is pretty straightforward, rep function repeats the first element ("=") scan() times, which is in fact the user's input.

a=scan();cat(rep("=",a),"(:)-")

EDIT:

cat(rep("=",scan()),"(:)-")

Slighly shorter answer, using scan() directly.

Alternatively,

cat(rep("=",scan()),"(:)-",sep="")

for a non-chopped snake (34 bytes)

\$\endgroup\$
2
  • \$\begingroup\$ I would say the sep="" is needed here, otherwise your snake looks like the one that traversed the railway: = = = (:)-. \$\endgroup\$
    – manatwork
    Aug 2, 2016 at 11:09
  • \$\begingroup\$ The snake produced here is indeed a bit minced, but OP's consigns only state that the length of the snake is the number of equal signs before the head. I will add the sep as a side note nonetheless ;) \$\endgroup\$
    – Frédéric
    Aug 2, 2016 at 11:13
3
\$\begingroup\$

Batch, 68 bytes

@set h=(:)-
@for /l %%i in (1,1,%1)do @call set h==%%h%%
@echo %h%
\$\endgroup\$
2
\$\begingroup\$

CJam, 13 11 bytes

qi'=*"(:)-"

Test it here.

-2 bytes thanks to quartata

\$\endgroup\$
1
  • \$\begingroup\$ You can do '=* instead of {'=}*. \$\endgroup\$ Jul 31, 2016 at 20:46
2
\$\begingroup\$

Perl 6,  16 15  12 bytes

{"{'='x$_}(:)-"}
{'='x$_~'(:)-'}
'='x*~'(:)-'

Explanation:

'=' x * # 「=」 string repeated by the only parameter 「*」
~        # concatenated with
'(:)-'   # the head

Usage:

# store it in the lexical namespace
my &snake = '='x*~'(:)-';

put snake 10;

# put ^5 .map: &snake;
put ^5 .map: '='x*~'(:)-';
==========(:)-
(:)- =(:)- ==(:)- ===(:)- ====(:)- =====(:)-
\$\endgroup\$
2
\$\begingroup\$

JAISBaL, 9 bytes

t=*Qb(:)-

Verbose:

# \# enable verbose parsing #\
push1 =        \# push = onto the stack #\
mul            \# multiply the top two values of the stack #\
popout         \# pop the top value of a stack and print it #\
print4 (:)-    \# print (:)- #\

Tested with JAISBaL-0.0.7 (The compiled .jar was just pushed, but the source has been up on git for a while)

\$\endgroup\$
2
  • \$\begingroup\$ Use a # before the language name to make it look like everybody else's. \$\endgroup\$ Jul 31, 2016 at 22:42
  • \$\begingroup\$ @MichaelKlein okay \$\endgroup\$ Jul 31, 2016 at 22:57
2
\$\begingroup\$

PowerShell v2+, 19 bytes

'='*$args[0]+'(:)-'

Full program. Takes input $args[0], uses string multiplication to construct the body, then string concatenation to tack on the head.

PS C:\Tools\Scripts\golfing> .\snakes-all-around.ps1 7
=======(:)-

PS C:\Tools\Scripts\golfing> .\snakes-all-around.ps1 77
=============================================================================(:)-
\$\endgroup\$
1
  • \$\begingroup\$ Kudos to you, I got as far as 26: "$("="*[int]$args[0])(:)-" \$\endgroup\$
    – Chirishman
    Aug 29, 2016 at 21:33
2
\$\begingroup\$

C, 46 45 43 bytes

saved 2 bytes thanks to owacoder! saved 3 bytes thanks to rici!

f(n){while(4-printf("=\0(:)-"+2*!n--));}

Try it on Ideone!

\$\endgroup\$
4
  • 3
    \$\begingroup\$ The mysterious --> operator strikes again. \$\endgroup\$
    – Leaky Nun
    Jul 31, 2016 at 20:37
  • \$\begingroup\$ @LeakyNun 46 bytes version was without the goes to operator, than I remembered the goes to operator ;) \$\endgroup\$
    – betseg
    Jul 31, 2016 at 20:41
  • 1
    \$\begingroup\$ You can remove the >0 and save two bytes. \$\endgroup\$
    – owacoder
    Jul 31, 2016 at 20:47
  • \$\begingroup\$ Slightly shorter: f(n){while(4-printf("=\0(:)-"+2*!n--));} \$\endgroup\$
    – rici
    Aug 2, 2016 at 2:38
2
\$\begingroup\$

Cheddar, 15 bytes (noncompeting)

n->'='*n+'(:)-'

A straightforward answer.

\$\endgroup\$
1
\$\begingroup\$

Sesos, 11 bytes

Hexdump:

0000000: aaaa5e a0f7b4 ed4cee 5d3b                         ..^....L.];

Try it online!

Assembler:

set numin
add 61
fwd 1
get
jmp,sub 1,rwd 1,put,fwd 1,jnz
add 40,put
rwd 1,sub 3,put
fwd 1,add 1,put
add 4,put
\$\endgroup\$
1
\$\begingroup\$

K, 17 Bytes

{,[x#"=";"(:)-"]}

Example;

f:{,[x#"=";"(:)-"]}
-1(f'!10);          /print out the result of calling f where x is 0 1 2 3 4 5....
(:)-
=(:)-
==(:)-
===(:)-
====(:)-
=====(:)-
======(:)-
=======(:)-
========(:)-
=========(:)-

Explanation;

{}                 /function x is implicit and is an int
x#"="              /take (#) x of "=" --> so 3#"=" gives "==="
,[x#"=";"(:)-"]    /comma is a join that takes 2 args --> ,[x;y] gives the concatination of x and y --> "a","abc" is the same as ,["a";"abc"] and gives "aabc"
\$\endgroup\$
6
  • \$\begingroup\$ What is -1(...);? \$\endgroup\$
    – Adám
    Aug 1, 2016 at 16:04
  • \$\begingroup\$ Save a byte: {(x#"="),"(:)-"} \$\endgroup\$
    – Adám
    Aug 1, 2016 at 16:06
  • \$\begingroup\$ Thanks Adam. -1 Prints to the console. -1"Hello"; will print Hello \$\endgroup\$
    – Chromozorz
    Aug 1, 2016 at 16:09
  • \$\begingroup\$ But isn't printing implicit? \$\endgroup\$
    – Adám
    Aug 1, 2016 at 16:11
  • \$\begingroup\$ Yes but it includes the double quotes... "====(:)-" rather than; =====(:)- \$\endgroup\$
    – Chromozorz
    Aug 1, 2016 at 17:22
1
\$\begingroup\$

Perl, 16 + 1 (-p flag) = 17 bytes

$_="="x$_."(:)-"

Needs -p flag, so run with :

perl -pe '$_="="x$_."(:)-"'
\$\endgroup\$
1
\$\begingroup\$

Minkolang 0.15, 12 bytes

"=(:)-"nD$O.

Try it here!

Explanation

"=(:)-"         Push this to the stack in reverse order - ["-",")",":","(","="]
       n        Take number from input
        D       Pop k and duplicate top of stack (the "=") k times
         $O.    Output whole stack as characters and stop.
\$\endgroup\$
1
\$\begingroup\$

Befunge-98, 24 bytes

Takes numerical input from the user, then prints the snake.

'=&:00pk:00gk,"-):("4k,@
\$\endgroup\$
1
\$\begingroup\$

Matlab / Octave, 22 bytes

@(n)[~(1:n)+61 '(:)-']

This is an anonymous function.

Try it on Ideone.

Explanation

Assume n= 5.

1:n produces the row vector [1 2 3 4 5].

~(1:n) negates each entry, so it gives [0 0 0 0 0].

...+61 adds 61 to each entry, so it gives [61 61 61 61 61]. 61 is the ASCII value of character =.

[... '(:)-'] concatenates that with the string '(:)-'. This automatically converts [61 61 61 61 61] into the string '=====' before the concatenation.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.