8
\$\begingroup\$

Write a program that outputs the number 0.

That's a bit trivial, isn't it?

Let the length of your program be \$S\$. Write a program that outputs the number 0. When your program is cyclically shifted \$i < S\$ bytes left, it should output \$i\$. To cyclically shift a string \$i\$ bytes left means to move the last \$i\$ bytes (including newlines) to the front, without changing the order.

For example, if your code is ABCD, then ABCD should output 0; DABC should output 1; CDAB should output 2; BCDA should output 3.

Rules

  • Your program must be at least 3 bytes long.
  • "Output" represents any acceptable output: on the stack, to STDOUT, as a return value, as an exit code, etc.
  • Your program is to not take any form of input.
  • This is , so shortest code wins.
\$\endgroup\$
  • \$\begingroup\$ (Not sure if source-layout is correct?) \$\endgroup\$ – Baaing Cow Apr 28 at 11:55
  • \$\begingroup\$ Given the example mentioned in the tag info, I'd say source-layout is indeed correct. \$\endgroup\$ – Arnauld Apr 28 at 12:00
  • 1
    \$\begingroup\$ I would say this is more interesting as an attempt to make the longest program that satisfies this restriction. Otherwise, a submission of length 3 is automatically the winner. \$\endgroup\$ – RGS Apr 28 at 12:07
  • \$\begingroup\$ @RGS Won't a brainfuck ".++++++++++++++++++++++++++++++++++++++ <infinitely many pluses>` work? \$\endgroup\$ – Baaing Cow Apr 28 at 12:09
  • 5
    \$\begingroup\$ @BaaingCow please update the challenge to allow the right-shift, comments can become cluttered and are a non-optimal way of providing specifications. \$\endgroup\$ – Jonathan Allan Apr 28 at 16:35
10
\$\begingroup\$

brainfuck, 3 bytes

.++

Try it online!

how it works: it's really simple, actually. The internal memory is initialized at 0 so we output (with .) the original cell and we get 0. By shifting the program, we are shifting memory increments (done with +) to before the output, so each byte shifted corresponds to one more increment, and hence we increase the counter.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ FGITW, you win. \$\endgroup\$ – user92069 Apr 28 at 12:26
  • \$\begingroup\$ @petStorm *LGITW to be honest; (luckiest instead of fastest) I haven't been around much and I was lucky enough to scroll past this challenge while it had no answers :) \$\endgroup\$ – RGS Apr 28 at 13:42
7
\$\begingroup\$

Jelly, 3 bytes

‘n¬

The implicit input of a Jelly program is 0:

Try ‘n¬ online! 0 incremented \$\neq\$ not(0) \$\implies0+1 \neq 1 \implies 1\neq 1 \implies 0\$
Try ¬‘n online! not(0) incremented \$\neq\$ 0 \$\implies1+1\neq 0 \implies 2\neq 0 \implies 1\$
Try n¬‘ online! (0 \$\neq\$ not(0)) incremented \$\implies(0\neq 1)+1 \implies 1+1 \implies 2\$

| improve this answer | |
\$\endgroup\$
5
\$\begingroup\$

Keg -hd, 3 bytes

210

Try it online!

How?

Keg only outputs the top item of the stack. These individual digits push themselves onto the stack. So for every cyclic shift, it outputs the character code of the top of the stack.

Program ->   Stack   -> Output last item
  210   -> [2, 1, 0] ->       [0]
  021   -> [0, 2, 1] ->       [1]
  102   -> [1, 0, 2] ->       [2]
| improve this answer | |
\$\endgroup\$
3
\$\begingroup\$

05AB1E, 3 bytes

Right-shifts instead of left-shifts.

>>g

Try it online!

Explanation

>>g : >>  Increment empty string twice
        g Find the length of the empty string (0)

>g> : >   Increment empty string
       g  Find the length (0)
        > Increment that (1)

g>> : g   Find length of empty string (0)
       >> Increment twice (2)
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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