# Tips for golfing in 2D languages

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).

• Shouldn't this question be on Meta? – Mr Lister May 17 '17 at 12:02
• There'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 May 17 '17 at 12:09
• @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 May 17 '17 at 15:59
• @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 May 17 '17 at 16:02

# 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

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


# 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)

• Doesn't your third example do ioiioiioi etc.? – ASCII-only May 17 '17 at 2:48
• @ASCII-only Indeed it does. Oops. – Conor O'Brien May 17 '17 at 3:15
• You 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 May 17 '17 at 5:54
• In 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 May 17 '17 at 8:19

# 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

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

• 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 May 17 '17 at 16:35

# 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.

# 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.

# 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.

# 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.

• This doesn't really seem like one tip. – Post Rock Garf Hunter May 17 '17 at 3:53
• @WheatWizard I kept them together because splitting them up made less sense to me, being all common program-layout idioms. – Conor O'Brien May 17 '17 at 4:01
• Why do you have the < in the ><> quine? – Jo King Feb 15 '18 at 12:33
• @JoKing I'm not particularly sure lol – Conor O'Brien Feb 15 '18 at 13:35
• 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 Feb 15 '18 at 13:57