Advent of Code in Befunge
Befunge is an esoteric two-dimensional stack language in which the program counter travels around the 80x25 program space. In addition to the stack, program space itself can be modified at runtime.
Instruction | Description |
---|---|
> |
Right |
< |
Left |
^ |
Up |
v |
Down |
? |
Random direction |
# |
Jump over |
0 ... 9 |
Push number |
" |
Toggle string mode |
: |
Dup |
\ |
Swap |
$ |
Pop |
+ |
Add |
- |
Subtract |
* |
Multiply |
/ |
Divide |
% |
Modulo |
! |
Logical NOT |
` |
Greater than |
_ |
Horizontal if |
` | ` |
g |
Get |
p |
Put |
& |
Input number |
~ |
Input character |
. |
Output number |
, |
Output character |
@ |
Exit |
other | In string mode, push character |
Solutions run with befrust and debugged with befungee.
Note: input must not end in a newline.
- Blue: standard input loop, exiting on EOF (-1).
- Purple: subtract ASCII value of
)
from input, resulting in-1
for(
and0
for)
. - Red: add the logical NOT of the result to itself, resulting in
-1
for(
and1
for)
. - Yellow: subtract the result from the total and output it.
- Blue: standard input loop, exiting on EOF (-1).
- Purple: check for newline (10).
- Red: check for "x".
- Yellow: on "x", discard the input and push 0 for the next number.
- Green: subtract ASCII value of "0" to convert input to number. Multiply existing number by 10 and add the input number.
- Cyan: on newline, the length, width and height will be on the stack. Replace each "l", "w", "h" in the pink and orange regions with their values.
- Pink: find the smallest side by calculating each and performing greater-than comparisons.
- Orange: calculate the areas of each side, add them, and multiply by two. Add the area of the smallest side. Add the area to the total, output, then push 0 for the next input value.
Note: non-standard Befunge, 255x255 program space.
- Blue: push starting coordinates
(128, 128)
, the center of a 255x255 program space. - Purple: temporarily store coordinates under blue region, use them to write a
1
into the program space, then restore the coordinates to the stack. - Red: input loop checking for newline (10).
- Yellow: compare input to "<", leaving the difference on the stack.
- Green: compare input to 2, the difference between "<" and ">", again leaving the difference on the stack.
- Cyan: compare input to 32, the difference between ">" and "^", again leaving the difference on the stack. It is assumed that any other value is "v".
- Pink: pop input value and add or subtract 1 to the X or Y coordinate.
- Orange: pop input and coordinates to empty the stack and enter the gray area.
- Gray: region of program space populated with
1
instructions by the input loop, then executed. - Lime: sum the entire stack full of 1s, output the result and exit.
Map of the houses Santa visited in program space:
No solution yet.
No solution yet.