## Advent of code day 5: A Maze of Twisty Trampolines, All Alike

Howdy! Or shall I say: Ho Ho Ho 🎅 🎄

Today I bring you:

## Advent of code day 5…Polyglot 🎁

Today’s puzzle proved to be a relatively simple one to be implemented in a number of programming languages, as the way the puzzle is set does not allow a crazy range of varied approaches. So I decided to do a polyglot approach in this one, just for a bit of fun.

## Part 1

An urgent interrupt arrives from the CPU: it’s trapped in a maze of jump instructions, and it would like assistance from any programs with spare cycles to help find the exit.

The message includes a list of the offsets for each jump. Jumps are relative: -1 moves to the previous instruction, and 2 skips the next one. Start at the first instruction in the list. The goal is to follow the jumps until one leads outside the list.

In addition, these instructions are a little strange; after each jump, the offset of that instruction increases by 1. So, if you come across an offset of 3, you would move three instructions forward, but change it to a 4 for the next time it is encountered.

For example, consider the following list of jump offsets: 0 3 0 1 -3 Positive jumps (“forward”) move downward; negative jumps move upward. For legibility in this example, these offset values will be written all on one line, with the current instruction marked in parentheses. The following steps would be taken before an exit is found:

(0) 3  0  1  -3  - before we have taken any steps.
(1) 3  0  1  -3  - jump with offset 0 (that is, don't jump at all). Fortunately, the instruction is then incremented to 1.
2 (3) 0  1  -3  - step forward because of the instruction we just modified. The first instruction is incremented again, now to 2.
2  4  0  1 (-3) - jump all the way to the end; leave a 4 behind.
2 (4) 0  1  -2  - go back to where we just were; increment -3 to -2.
2  5  0  1  -2  - jump 4 steps forward, escaping the maze.


So in this example you exit in 5 steps. The obvious question is: How many steps does it take to reach the exit?

### Python

I have been grabbing my input directly from the advent of code webpage using requests in order to avoid having loads ot ‘.txt’ files in the end. If you want to have a look at this helpers go to my GitHub repository.

Once I have the input I ensured that the content was in a list:

my_input = helper.get_data().split('\n')
offsets = [int(offset) for offset in my_input]


The next step is to define a function that would go over the elements of said list and keep a record of the number of jumps and update the value of the element in the list at which we were:

def trampoline():
i = 0
jump = 0
while 0 <= i < len(offsets):
offsets[i] += 1
jump +=1
i += offsets[i] - 1

return jump


by adding the line while 0 <= i < len(offsets): we ensure that the number of jumps is increased every time and the elements updated as long as we are not trying to jump outside the list.

### Julia

Coming from Python I have found Julia to be a very friendly language to learn. Of course I am always forgetting the end at the end of functions and loops, but a lot of programming should help me get of this.

Here is my solution following the same approach as above:

lines = map(parse, readlines(my_input));
function trampoline(offsets)
i = 1
jump = 0
elem = 0
while true
try
elem = offsets[i]
catch
break
end
offsets[i] += 1
jump += 1
i += elem
end
return jump
end


I found I had to use the try // catch statement for handling the Exception that would arise from exiting the array or better said, trying to reach an element outside the length of the array. Apart from that the approach is very much the same as the Python implementation.

### C++

Why C++? You ask. Well I have not been using C/C++ much recently and well I just thought why not?

auto trampoline(vector <int> offsets){
int i = 0;
int jump = 0;
while (i < offsets.size()){
int elem =  offsets[i];
jump++;
i += elem;
offsets[i]++;
}
return jump;
}


Note that I am only adding the actual function used for the ‘trampoline’ and ignored the rest of the program (loading libraries, complementary function to load the input into a vector and the main function).

Again this follows the same logic as the two other examples. Since this is getting boring pretty soon let’s just say that any of these solutions shall give us our first star ⭐️.

## Part 2

Now, the jumps are even stranger: after each jump, if the offset was three or more, instead decrease it by 1. Otherwise, increase it by 1 as before.

Using this rule with the above example, the process now takes 10 steps, and the offset values after finding the exit are left as 2 3 2 3 -1.

How many steps does it now take to reach the exit?

### Python

Phew! The second part can be easily incorporated in the function we constructed for Part 1. So this function could be easily modified to something like:

def trampoline_2():
i = 0
jump = 0
while 0 <= i < len(offset):
jump +=1
new = -1 if offset[i] >= 3 else 1
offset[i] += new
i += (offset[i] - new)
return jump


Voila!! Although it is quite simple to incorporate both parts in a single function taking as input the part that has to be solved and then use new = -1 if offset[i] >= 3 else 1 or offsets[i] += 1 depending on the part to be solved.

### Julia

Again the solution for part 1 can be adapted to solve part 2 by modifying it such that:

function trampoline2(offsets)
i = 1
jump = 0
elem = 0
while true
try
elem = offsets[i]
catch
break
end
offsets[i] += elem >= 3 ? -1 :1
jump += 1
i += elem
end
return jump
end


### C++

And yet again:

auto trampoline2(vector <int> offsets){
int i = 0;
int jump = 0;
while (i < offsets.size()){
int elem = offsets[i];

if (elem >= 3){
offsets[i]--;
}
else{
offsets[i]++;
}
jump++;
i += elem;
}
return jump;
}


Success!!! This gives us our second star! ⭐️

Tomorrow I will make sure to do a write up for a couple more puzzles as I am well behind. Although I must admit I do not intend to write every single solution, but you can always find the solutions in my GitHub repository.

See ya later!