Tips for golfing in 2D languages

18

What general tips do you have for golfing in 2 dimensional programming languages? I'm looking for tips which can be applied to code-golf problems and are specific to 2D programming languages, but not specific to any one language (Responses like "remove comments" and "use the M operation" are not answers).

Please post one tip per answer.

Post Rock Garf Hunter

Posted 2017-05-16T23:47:36.377

Reputation: 55 382

Shouldn't this question be on Meta? – Mr Lister – 2017-05-17T12:02:54.760

2There's a related meta question here: what is the size of a 2D program? Covered area? Area of the convex hull? Area of the axis-aligned containing rectangle? All might be better measures than some form of ASCII size. – MSalters – 2017-05-17T12:09:51.383

@MrLister Tips questions are on topic for the main site. If you look you can see that we have a good deal of questions under the tips tag already. – Post Rock Garf Hunter – 2017-05-17T15:59:33.247

@MSalters Just like every other programming language length is measured in bytes. Some language designers allow there 2D languages to be arranged in different manners, like Hexagony or Cubix, and thats up to them. – Post Rock Garf Hunter – 2017-05-17T16:02:22.397

Answers

19

Avoid horizontal gaps

Often, code will leave largs gaps of whitespace to the left hand side of the program, like so.

abc
  d
  e

This adds 4 bytes, when this could be avoided by left aligning.

cde
b
a

If you need to use large gaps of whitespace, try to make them vertical, instead of horizontal.

########
#      #
#      #
#      #

vs

####
#
#
#
#
#
#
####

ATaco

Posted 2017-05-16T23:47:36.377

Reputation: 7 898

11

Use one dimension when possible

Typically, simpler programs can be written on a single line. For example, the classic cat program could be:

>iv
^o<

But one could abuse the wrapping behavior and make this:

io

Or, in languages without such wrapping behavior:

> ?oi<

(Assuming ? doesn't pop.) In the case of a non-wrapping language, an explicit loop is often better.

With jump commands

In 2D languages with jump and conditional jump commands, a program could look like this:

abc >de?v;
    ^hgf<

This could also be:

abc de?!;hgf04&

(if ! is a trampoline, and & is jump to position)

Conor O'Brien

Posted 2017-05-16T23:47:36.377

Reputation: 36 228

Doesn't your third example do ioiioiioi etc.? – ASCII-only – 2017-05-17T02:48:14.270

@ASCII-only Indeed it does. Oops. – Conor O'Brien – 2017-05-17T03:15:11.263

1You should probably state what some of the less regular commands do. For example I don't know what io; do, and all I know is that ? doesn't pop. It seems like these are fish commands, but I don't think they are very standard. – Post Rock Garf Hunter – 2017-05-17T05:54:46.287

2In some 2D languages without conditional skipping commands (like Labyrinth) you can also often write looping single-line programs by conditionally terminating the program with a division by zero. – Martin Ender – 2017-05-17T08:19:28.433

10

Carriage Returns Are Bytes Too

The less 2D you can make it, the better. A carriage return is another no-op. Without ignoring the tips from @ATaco and @ASCII-only, try and keep the Y dimension as small as possible.

This

###
####
########

is better than

###
###
###
##
#
#
#
#

MickyT

Posted 2017-05-16T23:47:36.377

Reputation: 11 735

Carriage returns aren't always bytes. \n (line-feed) is a line-ending regularly used in left-aligned text on POSIX systems, although Windows and Mac OS (pre-macOS) use combinations of \n (line-feed) and \r (carriage-return). – wizzwizz4 – 2017-05-17T16:35:16.950

9

DRY (Don't Repeat Yourself)

While abstracting with functions is usually longer in Code Golf, it can really help for 2D languages. Try to rework your code so it can re-use the same snippet, entering/exiting it with two different branches of execution.

Cyoce

Posted 2017-05-16T23:47:36.377

Reputation: 2 690

8

Interleave paths

Usually in a 2D language there is an IP that moves according to direction commands. Since spaces are wasted bytes, it is almost always more efficient to rearrange the program so it moves closer to the left as often as possible, saving the need for unnecessary padding spaces.

ASCII-only

Posted 2017-05-16T23:47:36.377

Reputation: 4 687

2

Use mirrors

Mirrors can sometimes be used in two paths at the same time (each path bounces off one side of the mirror). This may not seem to help, but it may allow you to rearrange your program, or if you have a lot if direction changes they may be able to be replaced with fewer mirrors.

ASCII-only

Posted 2017-05-16T23:47:36.377

Reputation: 4 687

2

Memorize idioms

Here are a few "idioms" that do certain things, depending on the nature of the language.

Pseudo-linear code

If dynamic code generation is ever required, it may be of use to use the pseudo-linear code model:

v
\"line 1"
\"line 2"
.
.
\"line N"

Assuming \ and v mean what they usually do.

Infinite loop

In almost all 2D languages, >< is an infinite, unbreakable loop. If, for some reason, you need to do this, this is the best way, despite how nice this might look:

>v
^<

In fact, if you make your code a 1-liner, you could just use ^ or v, as such:

i?vo;

This v will send the IP to itself, wrapping around. You may still be able to use this approach in any instance where a directional command points to a series of (relative) no-ops.

Quine framework

Usually, languages with a string/quote framework can have a quine like this:

<quote><generate "><output stack><terminate>

For ><>, this would look like:

":1-r>o<#

Except this one exits with an error as termination. It is probably the shortest ><> quine, or, at least, the shortest one that I have found.

Conor O'Brien

Posted 2017-05-16T23:47:36.377

Reputation: 36 228

This doesn't really seem like one tip. – Post Rock Garf Hunter – 2017-05-17T03:53:46.063

@WheatWizard I kept them together because splitting them up made less sense to me, being all common program-layout idioms. – Conor O'Brien – 2017-05-17T04:01:53.807

Why do you have the < in the ><> quine? – Jo King – 2018-02-15T12:33:28.397

@JoKing I'm not particularly sure lol – Conor O'Brien – 2018-02-15T13:35:13.310

Also, the " ends up on the wrong side. The tip is good otherwise, I’ve used that general framework in a lot of my answers – Jo King – 2018-02-15T13:57:49.080