# Tag Info

## Hot answers tagged befunge

12

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< ,: ^_ $... 10 Python 3, will score later from collections import defaultdict from functools import lru_cache import sys NUMERIC_OUTPUT = True @lru_cache(maxsize=1024) def to_befunge_num(n): # 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: ... 9 When using a multi-line loop, try to use as much of it as possible: >1234....v ^ < vs >1234v ^....< 7 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. Example '* : v >$ .. @ Prints number in binary followed ...

6

Befunge-98, 4 ~,#@ Try it online! It works because ~ acts like r when there is no more input (credit to David Holderness for finding this in the funge-98 spec)

6

Don't forget that 0 is always on the stack. For example, this means that, with an empty stack, g is equivalent to 00g and p is equivalent to 000p.

5

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 1v v:< < >.1+:56+-| @ This one is fairly straight forward. Get a 1 on the stack, then print-increment-compare-loop until 11 is hit. ...

5

If you need to push a number larger than 15, use ' to fetch the ASCII value of the next character: '* to push 42 rather than: 4a*2+

4

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: 01-4*j01-*more ...

4

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: "a"9>1-:#v_@ ^\,:\< This can flatten be flattened into a single line by interspersing the loop sequence with bridge ...

4

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: "!iH",,,@ could be simplified to this: !iH",,,@ 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 ...

3

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 ...

3

4 solutions, 15 points I hope I didn't mess up the trailing whitespace anywhere when generating the hashes. Puzzle 1 (Score 3): de1de4c4c8 v v< > >1+:.:9!| @ Puzzle 3 (Score 5): 071cad0879 "!dlroW"v >,,,, ,,v , " , , o l , l , e " @ ^,"H" < ...

3

Output by exit code, where this is an allowed output form. If the challenge asks you to print one number, you can save a byte by ending the program with q instead of .@

2

Befunge, 10 ~:,a!#@_ You were thinking a bit too hard about it I think :)

2

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;<

2

Use the direction of the IP when dealing with _ or |, rather than using an extra character for !. Real example (from this post): #v~ ,>:!#@_ Can be changed to #v~ :<,_@#

1

As little white space as possible. If you have a 10x5 (50) code, can you make it into a 6x8 (48): some code goes here and it is exactly 50 characters goes to put a bi t of cod e here a nd count all of 4 0 chars!

1

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 ...

1

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 ,.

1

Sed, 5 chars So, even if this is not codegolf, here's a solution that will have a rather good codelength to score ratio, but not necessarily a good score. /^\$/d It simply removes blank lines.

1

since it seems no one could create 7 character program, here is what I did. it works at least with befunge! Befunge, 7 ~:,a`j@

Only top voted, non community-wiki answers of a minimum length are eligible