Welcome to the Shri++ Code Golf Task! In this challenge, you'll be working with a custom language interpreter called Shri++ to write the most compact solution possible. Your goal is to calculate the summation of numbers from 1 to 5 using Shri++ commands. There are bonus points for efficiency and creativity, so let's get started!
The directory provided to you will have the following files:
- shri++.c - The source code of the Shri++ interpreter.
- README.md - This file containing instructions.
- answer.spp - The Shri++ code file where you will write your solution.
You will first compile the shri++.c
file to create the executable shri++
. To do this, you will use gcc
, the GNU Compiler Collection.
- Open the terminal or command prompt.
- Navigate to the directory where shri++.c is located.
- Run the following command to compile the file:
gcc -o shri++ shri++.c
This command tells the compiler to take shri++.c
, compile it, and produce an executable named shri++
.
Once you've compiled the interpreter, you can run it and test its functionality.
-
To display the help message and see the available commands, use:
./shri++ -h
-
To run your answer.spp file for testing(which will contain your solution), use:
./shri++ answer.spp
The answer.spp file is where you will write your solution to the task. The goal is to calculate the sum of numbers from 1 to 5. The challenge is to write the code in as few characters as possible.
@
: Move the pointer to the right.#
: Move the pointer to the left.^
: Increment the value at the current pointer (starts from 0)._
: Decrement the value at the current pointer.:
: Output the value at the current pointer.;
: Input a value and store it at the pointer.{
: Start of a loop.}
: End of a loop.
- Shri++ uses a memory array, and the pointer starts at the first memory location with the value initialized to 0.
- As you move the pointer with
@
or#
, you can modify values using^
(increment) or_
(decrement). The value at each location is an integer, and you can output the value using:
.
Loops in Shri++ work similarly to basic loop structures, with {}
used to define the start and end of a loop.
- The loop continues as long as the value at the current pointer is non-zero.
- When the value at the memory location that the program is at in is zero
For example, here’s a simple loop that increments a value until it reaches 5:
^ Increment (now 1)
{
^ Increment (2, 3, 4, 5...)
}
To make this work correctly, you should have a memory location initiated with a value of say 3 and then decrement it at the end of each loop, this will make the loop run 3 times
@^^^
{@^^^^ #_}
this saves a memory location 1 as 3
then in the loop the pointer moves to memory location 2 and adds 4 and moves back to 1 then subtracts 1
this will continue 3 times till the value in memory location 1 is 0, at which point memory location 2 will have 4*3 = 12
-
10 Points for Generalizing to Any Number: Write a solution that can calculate the summation for any number, not just 5.
-
5 Points for the Fewest Characters: Try to write the most compact code possible to get the solution with the fewest characters.
-
Extra Points for Doing Both: If you manage to both generalize the solution and keep it compact, you earn additional points!
- Write your solution in
answer.spp
. - Compile and run the interpreter with your code as explained above.
- Ensure that the output is correct.
Submit the zip file on the form link given to you
Good luck, and may your code be as concise and elegant as possible!
For those new to working with the terminal or gcc, here are some helpful resources: