I spent a long plane ride coding this one up. I've written a pseudo befunge compiler which runs the befunge program, extracts basic blocks, and lays them out in a compact representation.
Link to the program.
When run on this 99 bottles program:
92+9* :. v <
>v"bottles of beer on the wall"+910<
^_ $ ...
Python 3, will score later
from collections import defaultdict
from functools import lru_cache
NUMERIC_OUTPUT = True
# Convert number to Befunge number, using base 9 encoding (non-optimal,
# but something simple is good for now)
assert isinstance(n, int) and n >= 0
if n == 0:
Need to drop a value after a conditional (e.g. because the other path depends on the value, but this one doesn't)? Instead of using >$ or $<, take advatage of the fact that you know the truth value of the variable and use _ instead to both change direction and pop stack.
'* : v >$ .. @ Prints number in binary followed ...
7 solutions, 26 points
I'm typing this on my phone, because I don't have access to my computer currently. I actually had to work out Puzzle 6 by pen & paper.
Puzzle 1 (Score 3): 4f52d5b243
This one is fairly straight forward. Get a 1 on the stack, then print-increment-compare-loop until 11 is hit.
Instead of using |, requiring another line (often with many extra spaces), try using j. For example:
01-`j@more code here
would stop if the number on top of the stack was negative and continue onward otherwise. If you need multiple characters, use n*j where n is the number of characters you need when the value passed to j is 0. Example:
In Befunge-93, it can often be advantageous to flatten a loop into a single line, with the loop section of code being executed in both directions.
For example, consider the code below, which outputs the letter a eight times:
This can flatten be flattened into a single line by interspersing the loop sequence with bridge ...
In Befunge-93, if the first thing you're pushing onto the stack is a string, you can often get away with dropping the opening quote. For example this:
could be simplified to this:
Try it online!
What's happening is the interpreter first tries to execute the characters in the unquoted string. The ! performs a harmless not, and the i ...
In Befunge-93, the character input command (~) can often be used as a shortcut for -1, since that is the value it returns on EOF.
As an example, the code below will output -1:
Try it online!
This is not recommended in production code, since when run in an interactive environment, the program would pause and wait for user input. And obviously if the ...
4 solutions, 15 points
I hope I didn't mess up the trailing whitespace anywhere when generating the hashes.
Puzzle 1 (Score 3): de1de4c4c8
Puzzle 3 (Score 5): 071cad0879
Don't forget that 0k does not execute the next instruction. This means that instead of doing:
;some boolean test;!jv;code if false;
;code if true;<
You can save a character by doing
;some boolean test;kv;code if false;
;code if true;<
As little white space as possible.
If you have a 10x5 (50) code, can you make it into a 6x8 (48):
and it is
put a bi
t of cod
e here a
all of 4
When pushing small numbers onto the stack, you can probably figure out easily enough that 45* will get you 20, and 67* will get you 42. When it comes to larger numbers, though, you really need a program that can calculate the most efficient representation for you.
The easiest option for this is Mike Schwörer's online interface for BefunRep. You simply type ...
Don't forget about the k operator. Instead of "!dlroW olleH",,,,,,,,,,,,@, do "!dlroW olleH"bk,@. Note that k does the operation on the cell that it is at so 9k, would print not 9 times but 10; 9 times with the k, and once with ,.