19
\$\begingroup\$

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.

\$\endgroup\$
4
  • \$\begingroup\$ Shouldn't this question be on Meta? \$\endgroup\$
    – Mr Lister
    Commented May 17, 2017 at 12:02
  • 2
    \$\begingroup\$ 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. \$\endgroup\$
    – MSalters
    Commented May 17, 2017 at 12:09
  • \$\begingroup\$ @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. \$\endgroup\$
    – Wheat Wizard
    Commented May 17, 2017 at 15:59
  • \$\begingroup\$ @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. \$\endgroup\$
    – Wheat Wizard
    Commented May 17, 2017 at 16:02

7 Answers 7

19
\$\begingroup\$

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

####
#
#
#
#
#
#
####
\$\endgroup\$
0
11
\$\begingroup\$

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

###
###
###
##
#
#
#
#
\$\endgroup\$
1
  • 1
    \$\begingroup\$ 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). \$\endgroup\$
    – wizzwizz4
    Commented May 17, 2017 at 16:35
11
\$\begingroup\$

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)

\$\endgroup\$
4
  • \$\begingroup\$ Doesn't your third example do ioiioiioi etc.? \$\endgroup\$
    – ASCII-only
    Commented May 17, 2017 at 2:48
  • \$\begingroup\$ @ASCII-only Indeed it does. Oops. \$\endgroup\$ Commented May 17, 2017 at 3:15
  • 2
    \$\begingroup\$ 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. \$\endgroup\$
    – Wheat Wizard
    Commented May 17, 2017 at 5:54
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ Commented May 17, 2017 at 8:19
9
\$\begingroup\$

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.

\$\endgroup\$
8
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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