-= Puzzle 10: The Banena™ Programming Language =-
Coming into work for your tenth day at BitFlop Laboratories you are greeted by a panicked coworker.
They explain that their "Banena™ Language" interpreter person has fallen ill and was subsequently fired, and they need you to step in and help them process some Banena™ language code.
You could say you are the new Internpreter. (Get it?)
"Banena™ language" or simply Banenalang is a register based language where the only way to write code is to use "ba", "be", "na" and "ne".
A Banenalang program consists of a series of instructions and labels, then when the program is run the instructions are executed in order from top to bottom. (We'll get back to labels later)
Each instruction (not label) starts with ba and is followed by a certain amount of nas.
The amount of nas determines the instruction type and the rest of the instruction consists of arguments for that instruction (like which registers to use, or what value to load).
Instruction arguments are seperated by nes. (they're kind of like the commas of the language)
The interpreter uses 16 registers, which are all initialized to 0 at the start of the program.
The registers are named from r0 to r15.
-= Instructions =-
Here is a brief overview of the instructions and their arguments surrounded by parentheses:
- 0
nas: Load immediate value into register. (val, dest_reg) - 1
na: Copy value from one register to another. (src_reg, dest_reg) - 2
nas: Add values from two registers and store result in a third register. (src_reg1, src_reg2, dest_reg) - 3
nas: Subtract values from two registers and store result in a third. (src_reg1, src_reg2, dest_reg) - 4
nas: Multiply values from two registers and store result in a third. (src_reg1, src_reg2, dest_reg) - 5
nas: Modulo values from two registers and store result in a third. (src_reg1, src_reg2, dest_reg) - 6
nas: Increment value in a register by 1. (reg) - 7
nas: Decrement value in a register by 1. (reg) - 8
nas: Jump to label. (label) - 9
nas: Jump to label if value in register is zero. (reg, label) - 10
nas: Jump to label if value in register is not zero. (reg, label)
If that went over your head, don't worry, your coworker has provided a few examples of how the instructions work.
They are as follows (the comments above explain how to read each instruction):
# Load value 2 into r1, ba(0na)ne(2na)ne(1na)
banenananena
# Copy value from r1 to r0, ba(1na)ne(1na)ne(0na)
bananenane
# Add values from r0 and r2, store in r1, ba(2na)ne(0na)ne(2na)ne(1na)
banananenenananena
# Subtract r1 from r3, store in r1, ba(3na)ne(3na)ne(1na)ne(1na)
bananananenanananenanena
# Multiply r1 and r1, store in r1, ba(4na)ne(1na)ne(1na)ne(1na)
banananananenanenanena
# Perform r0 % r6, store in r0, ba(5na)ne(0na)ne(6na)ne(0na)
bananananananenenanananananane
# Increment r1, ba(6na)ne(1na)
banananananananena
# Decrement r15, ba(7na)ne(15na)
bananananananananenanananananananananananananana
# Jump to label 1, ba(8na)ne(1na)
banananananananananena
# Jump to label 2 if r3 is zero, ba(9na)ne(3na)ne(2na)
bananananananananananenanananenana
# Jump to label 4 if r1 is not zero, ba(10na)ne(1na)ne(4na)
banananananananananananenanenananana
-= Labels =-
Labels are used to mark jump destinations in the code.
A label starts with be instead of ba and is followed by a certain amount of nas, the amount of nas determines the label's ID. For example, benanana is label 3 (be + 3 na).
Whenever a jump, conditional or unconditional, is executed, the program will jump to the next instruction after the label with the specified ID.
The label itself is not an instruction and does not count as a step in the program.
-= Extra Important Details =-
- The instructions are executed in order from top to bottom, the jump instructions can change the position of execution.
- All arithmetic is done with unsigned 16-bit integers and all register values are also unsigned 16-bit integers.
- All registers start with the value
0. - Modulo by zero results in
0instead of an error. - If an operation results in a value outside the range of a 16-bit unsigned integer, it wraps around. For example
65535 + 10 = 9and0 - 2 = 65534, this also means there are no negative numbers. - The program stops executing when it reaches the end of the instructions.
- Labels are preprocessed before execution starts, so jumps to labels will always find the correct instruction to jump to.
- Labels also do not count as instructions and are not executed.
"You got all that?" your coworker asks nervously.
You nod, trying to hide your confusion.
"Ok, then before we get to the main program, let's run a bunch of test programs to make sure you understand Banenalang."
Below are a few small Banenalang programs along with their expected final register states after execution, they will cover every instruction type and register usage.
It's recommended to go through them and make sure your implementation is correct before moving on to the main program.
Program 1:
banenanena
banenananenana
banenanananenanana
banananenanenananenananana
banananenananananenanananenanananana
bananenananananane
Expected final register states:
[6, 1, 2, 3, 3, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Program 2:
banenane
banenananananananananananena
banenanananananananananananananananananananananananananananananananananenana
banenanananananananananananananenanana
banenananananananananananananananananananananananananananananananananananananananenananana
banananenenananane
bananananenenanane
banananananenenanananane
bananananananenenane
banananananananane
banananananananane
bananananananane
banananananananena
banananananananenana
banananananananenanana
banananananananenananana
Expected final register states:
[3, 11, 34, 14, 39, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Program 3:
banenanane
banananananenenena
banananananenanenanenana
banananananenananenananenanana
banananenanananenanananenananana
banananenananananenananananenanananana
banananenanananananenanananananenananananana
banananenananananananenananananananenanananananana
banananenanananananananenanananananananenananananananana
banananenananananananananenananananananananenanananananananana
banananenanananananananananenanananananananananenananananananananana
Expected final register states:
[2, 4, 16, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 0, 0, 0, 0, 0]
Program 4:
banenane
banananananananananenananananananana
banenanena
be
banenanenana
banananananananananenanana
benana
banenanenanana
banananananananananena
benanana
banenanenananana
banananananananananenana
benananananananananana
banenanenanananana
bananananananananane
benananananananananananana
banenanenananananana
benanananananananana
banenanenanananananana
benananananananana
banenanenananananananana
banananananananananenananananananananana
bena
banenanenanananananananana
Expected final register states:
[1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0]
Program 5:
banenananenana
banenananananananananananenanananananananananananananana
be
banananenanenananena
banananananananenanana
bananananananananenananana
bananananananananenanananananananananananananana
banananananananananananenananananananananananananananane
Expected final register states:
[0, 20, 2, 10, 65526, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Program 6:
banenananananananananananananananananananananena
benanana
bananananananananena
banenananananananananananananananenana
benananana
bananananananananenana
banenananananananananananenanana
bena
bananananananananenanana
banananenenane
banananenananene
banananananananananananenanananena
banananananananananananenananenananana
banananananananananananenanenanana
banenanena
bananananananananane
banene
be
bananananananananena
bananananananane
bananananananananananenane
Expected final register states:
[44802, 65535, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Program 7:
banenanena
banenanananananananananananananananenanana
be
banananenenanenana
bananenane
bananenananena
bananananananananenanana
banananananananananananenananane
banenena
banenenana
Expected final register states:
[610, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
After going through all the test programs and confirming you understand how Banenalang works, your coworker nervously hands you the main program they need help with (your puzzle input).
After executing the main program, what is the value contained in register r0?