34
6
So, I wrote myself a one-liner which printed out a snake on the console. It's a bit of fun, and I wondered how I might condense my code...
Here's a (short) example output:
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Here's the specs:
- In each line, a single non-whitespace character (whichever you like) is printed to the console, initially with 29 to 31 spaces padding to the left of it.
- Each iteration, a random decision is made between these three actions
- The amount of padding decreases by 1
- The amount of padding remains the same
- The amount of padding increases by 1
Do this 30 times, to print 30-segment long a snake to the console.
The shortest answer in bytes wins.
Can we return an array of lines? Are leading/trailing spaces or newlines allowed? – Shaggy – 2018-02-20T12:07:00.357
@Shaggy I want to see the snake. I don't care if it's an array, as long as it's printed to a console and you can see it. – AJFaraday – 2018-02-20T12:09:13.270
"is printed to the console" implies a full-program only, but the default on PPCG is to allow functions too (which I recommend) – Jonathan Allan – 2018-02-20T12:09:20.810
1I assume trailing spaces on each line are allowed, right? – Luis Mendo – 2018-02-20T13:20:25.570
What should happen on a screen with less than 30 columns for text? Can we start with
cols
/2 padding and iteratecols
/2 times, or should the snake just wrap around? – kamoroso94 – 2018-02-20T22:32:45.3531What does "random" mean? Uniformly random? Random from any distribution with positive support on each of the three outcomes? Random from any distribution whatsoever? Do the results have to be different on each run of the program? – Nathaniel – 2018-02-21T09:02:02.410
1
Note that, by default, random does not mean uniformly so. For example, the second part of my brainfuck answer is (as of now) perfectly valid, despite it generating a straight line most of the time.
– Jo King – 2018-02-21T15:11:02.510Seriously has nobody made a golfing language yet that takes as input a Stack Exchange question ID number? – Lightness Races with Monica – 2018-02-21T15:41:14.950
1
Right now, there are two points that are unclear: 1) Does random mean uniformly random? (affects this answer) and 2) Must the output be different every time? (i.e., can the random number generator be unseeded? Affects this answer)
– James – 2018-03-02T20:41:12.033