Code Golfing Tips

Navigation

Recent site activity

PostScript

When golfing in PostScript you can use binary representations instead of ASCII for many common operations. It is also interesting to golf without allowing binary characters in the source code.

If you wish to golf with binary characters is easiest to write the code using ASCII and convert it to binary using an automated tool, for example:

http://code.google.com/p/postscript-golf-tool/

Documentation for the PostScript language can be found here [Adobe, PDF] or here [HTML].

You can use this web based application to convert submissions on Anarchy Golf from binary to plain text.

Reading from standard in

 * Read standard in and run it as a postscript program (9 bytes):

.runstdin

 * Read a space separated list of integers and store them in an array (11 bytes):

[.runstdin]

 * Read a fixed number of characters from standard in, and store them in a string on the stack. Also store a boolean showing EOF:
(%stdin)(r)file 1 string readstring

Example: reverse entire input : Solution, Solution (no binary)

* Read a single line with a maximum length of 99. Also store a boolean showing EOF.

(%stdin)(r)file 99 string readline

Example: slope lines : Solution, Solution (no binary)

* Same, for no binary, with maximum length of 256:

(%stdin)(r)file =string readline

Example: duplicate lines : Solution (no binary)

Writing to standard out

 * There are a few different operators that you can use:

=

=only

print

 * Writing a single character to standard out from an ASCII value:

{=}stopped pop 97 write

Example: stable partition : Solution, Solution (no binary)

No binary version:

( )dup 0 4 3 roll put =only

Example: stable partition : Solution (no binary)


[TODO: (%stdout)file]

Other PostScript Tricks

 * Generate a small random number, for example, between 0 and 4 inclusive (11 bytes, 14 bytes no binary):

realtime 5 mod

* Generate a random number where only certain bits can be set, for example, a random number from the set [0,1,8,9] (11 bytes, 14 bytes no binary):

realtime 9 and

Example: ASCII Stars : Solution, Solution (no binary)


 * Use def to store variables, or to avoid repeating yourself:

/a .runstdin def


 * Redefine '[' or ']' if you don't need their normal meaning. You don't need spaces between '[' and the previous and next tokens.

([).runstdin def

Examples:
ASCII StarsSolution (no binary)
numwarp : Solution (prettified)

 * Instead of using strings, sometimes you can use literals. This works if the string contains no spaces and you only need to print it using '=' or '=only'.

/Hello =

Example: 196 algorithm : Solution (no binary)


 * Print a blank line:

/ =

 * Concatenate two strings:

(ab)(cd)concatstrings

Example: Word frequency count : Solution, Solution (no binary)

 * Compress a PostScript code
You can compress a file:

(output-file-name)(w)file /zlibEncode filter
(input-file-name)(r)file input-file-length string readstring pop
writestring

and you can exec the compressed file

currentfile /zlibDecode filter cvx exec commpressed-code
or
currentfile /zlibDecode filter run commpressed-code

Example: Text Compression : Solution

 * Sorting array (Ghostscript 8.00 or higer):

[9 1 8 2 7 3 6 4 5]{lt}.sort

Example: Set symmetric difference : Solution, Solution (no binary)

 * Homogeneous arrays

Example: numwarp : Solution (prettified)

 * Define multiple functions using [...>>begin

Example: numwarp : Solution (prettified)