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 8 years ago

Reputation: 55 382

Shouldn't this question be on Meta? – Mr Lister – 8 years ago

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 – 8 years ago

@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 – 8 years ago

@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 – 8 years ago

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 8 years ago

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 8 years ago

Reputation: 36 228

Doesn't your third example do ioiioiioi etc.? – ASCII-only – 8 years ago

@ASCII-only Indeed it does. Oops. – Conor O'Brien – 8 years ago

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 – 8 years ago

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 – 8 years ago

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 8 years ago

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 – 8 years ago

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 8 years ago

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 8 years ago

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 8 years ago

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 8 years ago

Reputation: 36 228

This doesn't really seem like one tip. – Post Rock Garf Hunter – 8 years ago

@WheatWizard I kept them together because splitting them up made less sense to me, being all common program-layout idioms. – Conor O'Brien – 8 years ago

Why do you have the < in the ><> quine? – Jo King – 8 years ago

@JoKing I'm not particularly sure lol – Conor O'Brien – 8 years ago

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 – 8 years ago