--- Day 10: Scripting snag ---
"Yep, that's right," Patch gloats after showing the head packaging elf your findings.
"Hmm," the head packing elf mumbled, scratching behind one pointed ear. "I do concede that we would need a rather large number of sleighs to make this work."
Patch raised an eyebrow. "Large enough that the reindeer union would go on strike before we got through the first dozen flights."
A murmur of agreement rippled through the other elves. The head elf sighed, defeated. "Well... perhaps the packing script needs a repair."
At that, a nervous silence fell across the workshop. You blink. "Can't we just update it?"
The elves all recoil, while the head packing elf lets out a little chuckle. "Well of course, if you're volunteering, that would be great!"
Patch leans in and whispers, "The packing algorithm is written in JollyScript."
You stare blankly, unsure of what to expect.
"It's quite... err... optimised," says Patch. "There are certain constraints that need to be respected to ensure that the program will run everywhere -- on sleighs, on packaging machines, or wherever it's needed." She beckons you over to a nearby packing terminal, enters some commands to bring up the packing arrangement source, and quickly homes in on the issue -- as it turns out, the terminal had been issuing warnings about the broken component for a while, but they had been ignored in the struggle to get the packages ready on time.
"Yep, that'll be the problem."
You peer over Patch's shoulder to see what appears to be a Christmas tree! Looking closer in disbelief quickly snaps you out of the illusion -- to your dismay that the Christmas tree was actually an unintelligible smorgasbord of symbols. It's immediately obvious why no-one volunteered.
Patch gets up from the workstation, and hands you an antique-looking JollyScript manual from the back of the terminal. It looks concerningly concise. "Unfortunately, I can only get you this far -- only a small portion of the elf population choose to specialise into JollyScript, and I am certainly not one of them. Good luck!"
JollyScript is an enterprise-grade language for intelligent elves. It's so festive, that every JollyScript program is shaped like a Christmas tree!
JollyScript operates on a stack of 4-bit integers, also known as nibbles.
The program is executed via a cursor on a 2-dimensional grid. The cursor starts at the asterisk symbol *, usually at the top of the tree, and moves downwards diagonally.
It can go either left or right, but its initial direction is left.
After the cursor encounters a command, it continues in the same direction as before, unless the command changes the direction. Here are the commands for JollyScript:
/: Set the direction to left.\: Set the direction to right.^: Fork the program into two serial (non-parallel) threads, one going left, and one going right. Run the left in the left direction completely. When that terminates, run the right branch.~: Terminate this thread.+: Pop a, then pop b. Push a + b onto the stack.-: Pop a, then pop b. Push b - a onto the stack.:: Duplicate the top of the stack.%: Swap the top two values of the stack.The following is an example program in JollyScript.
*
{
^ \
^ \ ^
^ - ? "
4 % . n #
5 . } n "
~~~~~~~~~~~~~
|_|
This is the output of the example program.
41
#00
As you grab your tools and head to the JollyScript Console, the elves cheer behind you. "You're the only one who can fix this! We believe in you!"
You've been tasked with debugging a program that's supposed to print the nth triangular number mod 16, given the digit n as console input.
The elves perform some preliminary analysis to reveal some facts that should prove useful when debugging:
Exactly three characters in the program are wrong.
The correct program always exits on the ~ on the 7th line.
The issues mostly seem to be cursor flow related - the majority of the inner tree logic is correct.
It may be useful to note that there exists source code for JollyScript interpreters online.
Storage space is very important - the authors have golfed the program as much as they can, but some characters at the bottom of the tree needed to be left in to maintain the Christmas tree appearance. From the analysis, you know that only the characters below will never be executed in the fixed larger program - all of the rest will be (for the majority of inputs).
/
/
/
/ /\/ \
/~ {=''= ~~~ ~\
//---\\
Can you find the bugs?
Your answer should be the locations of the incorrect characters, in the format (line number 1, column number 1), (line number 2, column number 2), (line number 3, column number 3), in order of ascending line number.
$: Pop and discard the top of the stack.?: Look at the top of the stack. If it is zero, set the direction to left. Otherwise, decrement the top of the stack, and set the direction to right.0-9, A-F: Push a hexadecimal digit onto the stack..: Print the top of the stack as a hexadecimal digit.": Toggle printing mode. Characters encountered will instead be printed until printing mode is turned off.n: Print a newline.{: Remember this cursor location (but not the direction).}: Go back to the location you remembered, continuing in the same direction.