# Keg/Golfing

This page tries to teach the reader how to golf programs in Keg, as the official tutorial did not mention golfing techniques. There are not many tips, so feel free to add some.

## Problem #1. Golf the Hello, World! program.

The original one in the tutorial:

```Hello\, World\!^(!|,)
```
• Tip #1. Try to check whether a part in a command sequence can be deleted. (For example, two consecutive &'s can be deleted. The running times of for loops like the one in the tutorial can be deleted.)

The program after simplification:

```Hello\, World\!^(,)
```
• Tip #2. Do automatic-implemented jobs manually. (Reverse a message to push onto the stack manually instead of letting Keg reverse it. This saves a byte. It won't always help.)

The program after simplification:

```\!dlroW \,olleH(,)
```
• Tip #3. The whole stack will be outputted on the end of the program.

The final program after golfing:

```Hello\, World\!
```

## Problem #2. Golf the Deadfish interpreter.

Original one:

```0&{&:&4444***=[&_0&]&:&0<[&_0&] \>\>25*,,,,?:o=[&:.&]:i=[&1+&]:d=[&1-&]:s=[&:*&]}
```
• Tip #2. Do automatic-implemented jobs manually. (Calculate the result by person instead of letting Keg do it.)

4*4*4*4=4*2*4*2*4=8*8*4 Revised one:

```0&{&:&884**=[&_0&]&:&0<[&_0&] \>\>25*,,,,?:o=[&:.&]:i=[&1+&]:d=[&1-&]:s=[&:*&]}
```
• Tip #2. Do automatic-implemented jobs manually. (If a number exists in the ASCII alphabet, then use it.)

Revised one:

```0&{&:&\@4*=[&_0&]&:&0<[&_0&] \>\>25*,,,,?:o=[&:.&]:i=[&1+&]:d=[&1-&]:s=[&:*&]}
```
• Tip #5. Escaping a character wastes another byte.

The wanted number is 256, which is 255+1. 255 in the ASCII alphabet is `ÿ`.
After golfing:

```0&{&:&1ÿ+=[&_0&]&:&0<[&_0&] \>\>25*,,,,?:o=[&:.&]:i=[&1+&]:d=[&1-&]:s=[&:*&]}
```

Note: if you encode `ÿ` as in Unicode, it will still be 2 bytes.

• Tip #6. Tips on pushing 10 and 8.

The original program includes `25*`, which is 3 bytes long.
Note that Keg pushes everything that is not a command onto the stack except for the newline (and the tab) . That is why this newline character needs an escaping sequence. Program after golfing:

```0&{&:&ÿ1+=[&_0&]&:&0<[&_0&] \>\>\
,,,,?:o=[&:.&]:i=[&1+&]:d=[&1-&]:s=[&:*&]}
```

(Note: the tab character cannot be escaped to be pushed, as the command 8 already does this job.)

• Tip #7. Combine two commands with the same body

It is okay to combine the results of the conditionals using +(which acts like a logical operator). (It can never be 2, as a number cannot be both equal to 256 and less than 0.) This is the program after golfing:

```0&{&:&ÿ1+=&:&0<+[&_0&] \>\>\
,,,,?:o=[&:.&]:i=[&1+&]:d=[&1-&]:s=[&:*&]
```
• Tip #10. Try not to use the accumulator.

Not using the accumulator makes golfing programs easier. This is an alternate program:

```0'{':"ÿ1+=':"0<+['_0"] \>\>\
,,,,?:o=[':."]:i=['1+"]:d=['1-"]:s=[':*"]}
```

Wait, the `'` on the beginning of the program can be deleted. This leads to another byte being golfed:

```0{':"ÿ1+=':"0<+['_0"] \>\>\
,,,,?:o=[':."]:i=['1+"]:d=['1-"]:s=[':*"]}
```
• Tip #7. Combine two commands with the same body

It seems that `':"` was repeated twice in the program. There is a simpler way to replace it: Duplicate twice, and then swap the top 2 elements.

```0{'::"ÿ1+=\$0<+['_0"] \>\>\
,,,,?:o=[':."]:i=['1+"]:d=['1-"]:s=[':*"]}
```
• Tip #8. Let Keg autocomplete brackets

Keg will balance any unclosed brackets automatically, so they can be left off

```0{'::"ÿ1+=\$0<+['_0"] \>\>\
,,,,?:o=[':."]:i=['1+"]:d=['1-"]:s=[':*"
```

## Tips that do not seem to fit anywhere but still relate to golfing

• Tip #9. How to get characters on the ASCII alphabet?

Use print() to print out characters using escape sequences. Copy the outputted character to the program. (If this computer can run Keg, it definitely can run Python.)
0x07 might be hard to get, since it is the bell character. Here is a copy of this character for copying: `�`
Pushing any single digit (0 through 9) as a character isn't hard -- simply escape it using ` \ `

• Tip #10. Try to not use the accumulator. Instead, use `'` and `"` and the bottom of the stack for accumulator-like storage. This will make getting the code shorter easier. See  for an example of this.