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

1
\$\begingroup\$

x86 COM, 29 bytes

8A 0E 82 00 80 E9 30 B4
02 B2 3D CD 21 49 75 FB
BA 18 01 B4 09 CD 21 C3
28 3A 29 2D 24

aka

16DA:0100 8A0E8200      MOV     CL,[0082]
16DA:0104 80E930        SUB     CL,30
16DA:0107 B402          MOV     AH,02
16DA:0109 B23D          MOV     DL,3D
16DA:010B CD21          INT     21
16DA:010D 49            DEC     CX
16DA:010E 75FB          JNZ     010B
16DA:0110 BA1801        MOV     DX,0118
16DA:0113 B409          MOV     AH,09
16DA:0115 CD21          INT     21
16DA:0117 C3            RET
16DA:0118 283A          SUB     [BP+SI],BH // Snake Head
16DA:011A 292D          SUB     [DI],BP    // Snake Head
16DA:011C 2400          AND     AL,00      // Snake Head

Name it "snake.com"

Call it from cmd (tested with Win XP), parameters 1 to 9 supported:

snake 5

results in

=====(:)-
\$\endgroup\$
1
\$\begingroup\$

Brainfuck, 53 bytes

++++[->+++++<]>[->+++>++<<],>+<[->.<]>>.<---.>+.++++.

Explanation:

++++[->+++++<]    We build a 20 as 5*4
>[->+++>++<<]     We build a 60 and a 40 in two neighboring cells out of the 20, by multiplying the 20 with 3 and with 2
,>+<[->.<]        Read input, and print 61 ('=') as many times as the input idicates
>>.<---.>+.++++.  Print out the rest, "(:)-", noting how close each character is to either 60 or 40.
\$\endgroup\$
1
\$\begingroup\$

Lua, 28 Bytes

Takes input in the form of a command-line argument and outputs via its return value. Even if it uses a return statement, it is still a full program :).

return("="):rep(...).."(:)-"
\$\endgroup\$
1
\$\begingroup\$

LiveScript, 15 bytes

->'='*it+'(:)-'
\$\endgroup\$
1
\$\begingroup\$

BASIC, 30 bytes

Let's keep it simple in BASIC. QB64 and TRS-80 Extended Color BASIC both tie, since TRS-80 needs a line number but doesn't need the space after INPUT, and it won't let you use INPUT in immediate mode. One liners are rare on a 32x16 screen.

TRS-80: (should also work on Microsoft GW-BASIC/BASICA)

0INPUTL:?STRING$(L,"=");"(:)-"

enter image description here

QB64:

INPUT L:?STRING$(L,"=");"(:)-"
\$\endgroup\$
2
  • 1
    \$\begingroup\$ GW-BASIC seems to require a space after INPUT. \$\endgroup\$ Jan 12 at 23:19
  • \$\begingroup\$ My old Model 1 Level 1 could use abbreviations like "I." for "INPUT" but I don't think Level 1 BASIC had STRING$. \$\endgroup\$ Feb 7 at 19:51
1
\$\begingroup\$

C#, 42 bytes

string w(int n)=>new string('=',n)+"(:)-";

Try it here!

\$\endgroup\$
3
  • \$\begingroup\$ Does this code print the string? \$\endgroup\$
    – Jordan
    Aug 1, 2016 at 20:49
  • \$\begingroup\$ @Jordan: No, this is a function that returns the string. \$\endgroup\$
    – raznagul
    Aug 2, 2016 at 15:02
  • \$\begingroup\$ The requirements say solutions should "print a snake." I'm not sure if just returning a string is kosher. \$\endgroup\$
    – Jordan
    Aug 2, 2016 at 15:13
1
\$\begingroup\$

Java 1.7, 46 bytes

A recursive (and shorter) alternative to the iterative and 'replace' versions.

String f(int n){return n>0?"="+f(--n):"(:)-";}

Run it here.

This function first builds the n tail elements recursively, then appends the head.

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

Dyalog APL, 13 bytes

'(:)-',⍨⍴∘'='

'(:)-',⍨ the snake head appended to
⍴∘'=' n repetitions of a body segment

TryAPL online!

A proper function. The equivalent program is '(:)-',⍨⎕⍴'='.

\$\endgroup\$
2
  • \$\begingroup\$ You forgot the byte count. \$\endgroup\$
    – m654
    Aug 1, 2016 at 14:34
  • 3
    \$\begingroup\$ @m654 The snake nibbled it! \$\endgroup\$
    – Adám
    Aug 1, 2016 at 15:56
1
\$\begingroup\$

Brainfuck, 47 46 44 bytes

+[+<-[<]>->>++]<<++<,[->.<]<.>>---.<<+.++++.

Try it online! Takes input as a single byte value (e.g. space is 32). The program requires an interpreter with 8-bit cells and a tape not bounded on the left.

+[+<-[<]>->>++] (found by a bit of brute forcing) initialises the tape to

[253, 0, 0, 232, 0, 0, 40, 0, 59, 86, 0]
                                      ^

We then increment the 59 to 61 with <<++ and read the input char with <,. [->.<] then outputs the required number of =, and <.>>---.<<+.++++. outputs the (:)- portion, making use of the aforementioned 61 as well as the 40 two cells away.

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

Fish (><>), 33 bytes:

An upside down F cause ><>. Takes integer as command line argument by running ./fish.py snake.fish -v [number].

\
"
-
)
:
(!;!?lo<
=
"
>{1-:?!^}:

Try it here! The best explanation is to watch it with animation on the website.

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

Befunge 93, 25 bytes

Try it Online!

1+:&`#v_"=",
"(:)-"<@,,,,

Not compatible with '98, because the & character acts like r when there is no input, instead of repeating the last token

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

C++14, 59 bytes

As unnamed lambda:

#import<string>
[](int n){return std::string(n,61)+"(:)-";}

Usage:

#include<iostream>

#import<string>
auto f=
  [](int n){return std::string(n,61)+"(:)-";}
;

int main(){
  std::cout << f(2) << std::endl;
  std::cout << f(5) << std::endl;
  std::cout << f(10) << std::endl;
}
\$\endgroup\$
1
\$\begingroup\$

Turing machine code, 242 bytes

0 * * r 0
0 _ _ l A
A 0 9 l A
A 1 0 r 1
A 2 1 r 1
A 3 2 r 1
A 4 3 r 1
A 5 4 r 1
A 6 5 r 1
A 7 6 r 1
A 8 7 r 1
A 9 8 r 1
A _ _ r 3
1 _ = l 2
1 * * r 1
2 = = l 2
2 * * * A
3 = = r 4
3 * _ r 3
4 = = r 4
4 _ ( r 6
6 _ : r 7
7 _ ) r 8
8 _ - * halt

Try it online.

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

Common Lisp, 29 bytes

(format t"~v{=~}(:)-"(read)1)
\$\endgroup\$
1
\$\begingroup\$

Befunge-98 (FBBI), 22 18 bytes

Saved 4 bytes by putting the "-):(" at the beginning. This let me get rid of the separate printing of the head, and also the ' for pushing the =.

"-):(="&k:$$>:#,_@

Try it online!

Explanation

"-):(="                   Pushes '-):(=' to the stack - the head reversed
       &k:                Duplicates the = N+1 times (now there are N+2 '='s on the stack)
          $$              Discards 2 so that there are N '='s
            >:#,_         Prints all the top non-zero chars on the stack (the whole snake)
                 @        Exits
\$\endgroup\$
1
\$\begingroup\$

VBA Excel, 23 bytes

using Immediate window and cell [A1] as input

?string([A1],"=")"(:)-"

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

Vyxal, 11 9 bytes

\=*\:øb‹+

Try it Online!

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

Pyth, 10 bytes

+*\=Q"(:)-

Test suite.

+*\=Q"(:)-
    Q        get input
 *\=         repeat "=" that many times
+    "(:)-   append "(:)-"
\$\endgroup\$
0
\$\begingroup\$

C++, 106 103 bytes

#include <iostream>
void s(int n){for(int i=0;i<n;i++){std::cout<<"=";}std::cout<<"(:)-"<<std::endl;}

Usage

#include <string>
int main(int argc, char *argv[]) {
    int length = std::stoi(argv[1]);
    s(length);
    return 0;
}

Ungolfed

#include <iostream> // for printing
void outputSnake(int length) {
    for (int i = 0; i < length; i++) { // iterates over length of snake
        std::cout << "="; // prints an equals sign
    }
    std::cout << "(:)-" << std::endl; // prints rest of snake
}
    
    

Thanks @LeakyNun for shaving off 3 bytes.

\$\endgroup\$
3
  • \$\begingroup\$ Replace i!=n+1 with i<n, and I am not sure if std:: is needed. \$\endgroup\$
    – Leaky Nun
    Jul 31, 2016 at 20:15
  • \$\begingroup\$ @LeakyNun I would have to add "using namespace std;" to remove the std portion, but i'll replace that not equal sign \$\endgroup\$ Jul 31, 2016 at 20:16
  • \$\begingroup\$ Couldn't you replace the for-loop with ´while(n-->0)´? \$\endgroup\$
    – Doddy
    Aug 2, 2016 at 6:10
0
\$\begingroup\$

Java, 133 bytes

public class Snake {
    public void s(int n){
        for(int i=0;i<n;i++){
            System.out.print("=");
        }
        System.out.println("(:)-");
    }
}
\$\endgroup\$
5
  • 1
    \$\begingroup\$ This site says that your code is 165 bytes. \$\endgroup\$
    – acrolith
    Jul 31, 2016 at 20:39
  • 1
    \$\begingroup\$ If you prefer to just share the method then don't include the class at all, otherwise provide a main in order to provide a complete program. Anyway, class don't need to be public. Class name could be shorter. Also, you could save some bytes by concatenating the "=" and printing them all at the end.Don't include {} or unnecessary spaces. Here are these suggestions: pastebin.com/8jLNL65V \$\endgroup\$
    – Master_ex
    Jul 31, 2016 at 20:51
  • \$\begingroup\$ @daHugLenny I used wc -c on GNU/Linux to count the bytes. \$\endgroup\$ Jul 31, 2016 at 21:08
  • \$\begingroup\$ Assuming tabs (because SE sites convert them to spaces) the count actually turns out to 132 bytes. (And sorry @Master_ex, it seems like LeakyNun has already used what you said in their answer so it can't be used here.) \$\endgroup\$
    – Value Ink
    Aug 1, 2016 at 5:24
  • \$\begingroup\$ @ValueInk, if you referring to LeakyNun's java answer (the traditional one) it seems that it was posted after my comment and in my pastebin link I save some more bytes in the for loop, but no problem, I just wanted to help :-) \$\endgroup\$
    – Master_ex
    Aug 1, 2016 at 6:58
0
\$\begingroup\$

><>, 26 23 bytes

:?!v"="o1-
oo;>"-):("oo

-3 bytes thanks to Leaky Nun

Test it here. (you have to input the code manually)

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

Javascript (using external library) (39 bytes)

n=>_.Range(0,n).Write("=",x=>"")+"(:)-"

Link to lib: https://github.com/mvegh1/Enumerable

Code explanation: Create an array of n elements, and Write a concatenated string with "=" as the delimiter and an empty string as the current element. Then concatenate the head of the snake

enter image description here

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

Matricks, 24 bytes

Very simple, just wanted to show off the new matrix definition feature.

m61:1:n;;a<40,58,41,45>;

Run with python matricks.py snek.txt [[]] <input> --asciiprint

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

Nim, 46 bytes

import strutils,future
n=>repeat("=",n)&"(:)-"

This is an anonymous function that must be passed to a procedure to be used. To test, use the testing program below:

import strutils,future
proc test(f: int -> string) = echo f(#[Your input here]#)
test(n=>repeat("=",n)&"(:)-")
\$\endgroup\$
0
\$\begingroup\$

Clojure, 35 bytes

#(str(apply str(repeat %\=))"(:)-")

This is an anonymous/unnamed function that takes a single integer as an argument and returns a string.

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

PL/SQL: 28 bytes

rpad('=','&n','=')||'(:)-');
\$\endgroup\$
1
  • \$\begingroup\$ 1) Tried it? There is no change to work due to the extra ). 2) No idea about the dialect, but wouldn't lpad('(:)~',&n+4,'='); work? 3) This is just a snippet, not a valid solution. \$\endgroup\$
    – manatwork
    Aug 1, 2016 at 13:40
0
\$\begingroup\$

VBA, 37 bytes

Function S(L):S=String(L,"=")&"(:)-"

and enter gives the End Function. Immediate window of VBA editor:

?S(41)
=========================================(:)-
\$\endgroup\$
0
\$\begingroup\$

Brainfuck, 67 bytes

Code:

>++++++[>++++++++++<-]>+<,[->.<]<++++[>++++++++++<-]>.>---.<+.++++.

Explanation:

> At #1
++++++[>++++++++++<-]>+< Put '=' in #2
, Input into #1
[->.<] Output '=' #1 times thereby emptying #1
<++++[>++++++++++<-]>. Put '(' in #1 and output

> At #2
--- Change #2 to ':'
. Output

< At #1
+.++++. Change to '(' and 'minus' and output

Try it online!

Input works using the corresponding ASCII characters - that is, to input 33 you would type !. For convenience, you can use \d as the input in the interpreter linked, where d is any non-negative integer.

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

Julia, 15 bytes

x->"="^x*"(:)-"

Wow, this is short.

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

Racket, 46 bytes

(λ(n)(string-append(make-string n#\=)"(:)-"))

Pretty terrible golfing language :)))

\$\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.