## DEV Community

Fenriuz

Posted on • Originally published at alexvalle.dev on

# AdventJS 2023: Day 5 Challenge

The solution to the Challenge #5 of AdventJS 2023

The solution to the previous challenge

The solution to the next challenge

## Challenge Description

Santa ðŸŽ… is testing his new electric sled, the CyberReindeer, on a North Pole road. The road is represented by a string of characters, where:

S = Santa's Sled

• = Open barrier | = Closed barrier Example of a road: S...|....|.....

Each unit of time, the sled moves one position to the right. If it encounters a closed barrier, it stops until the barrier opens. If it is open, it goes through directly.

All barriers start closed, but after 5 units of time, they all open forever.

Create a function that simulates the sled's movement for a given time and returns an array of strings representing the state of the road at each unit of time:

``````const road = 'S..|...|..'
const time = 10 // units of time

/* -> result:
[
'S..|...|..', // initial state
'..S|...|..', // sled stops at the barrier
'..S|...|..', // sled stops at the barrier
'...S...*..', // barrier opens, sled advances
'...*...S..', // passes through the open barrier
]
*/
``````

The result is an array where each element shows the road at each unit of time.

Take into account that if the sled is in the same position as a barrier, then it takes its place in the array.

## Analysis

To solve this challenge, we must move the letter `S` within the string and stop it when it finds a barrier (`|`). The barrier will open (`*`) after 5 units of time. We need to store the journey of the letter `S` in an array until the units of time run out.

### Inputs

1. Road (`road`): The original string through which the sled will make its journey.

2. Time (`time`): A number with the units of time.

### Output

• An array of the history of the sled's journey

### Considerations

• We must stop the sled when it encounters a barrier and store it in history even if it does not move from there.

• When the sled passes over an open barrier (`*`), it will replace it in the string and once it has passed, the open barrier (`*`) must be put back.

## Solution

The initial position of the sled is identified at the beginning. The function iterates for each unit of time, advancing the sled if there is no closed barrier ahead. On the fifth unit of time, all barriers open permanently. The state of the road is updated and saved in an array at each iteration, reflecting the position of the sled and the state of the barriers. The final result is an array showing the state of the road at each moment of the given time.

### Code

``````/**
* Simulates the movement of Santa's sled on a road for a given time.
*
* @param {string} road - Representation of the road with '.', 'S', '|', and '*'.
* @param {number} time - Number of units of time to simulate.
* @return {string[]} - Array representing the state of the road at each unit of time.
*/
// Stores the state of the road at each unit of time

// Finds the initial position of the sled

// Character that will be replaced by the sled when it moves
let replacedChar = ".";

// Iterates for each unit of time, minus one, since the initial state is already included
for (let i = 0; i < time - 1; i++) {
// Gets the current state of the road

// On the fifth iteration (unit of time 5), all barriers open
if (i === 4) {
}

// Checks if the next position of the sled is not a closed barrier
if (currentRoadState[sledPosition + 1] !== "|") {
// Prepares the new sled position
// concatenating it to the element that previously was in that position
const newSledPosition = `\${replacedChar}S`;

// Updates the character replaced by the sled before being replaced

// Builds the new state of the road with the sled moved one position
const lastPart = currentRoadState.substring(sledPosition + 2);
currentRoadState = firstPart + newSledPosition + lastPart;

sledPosition += 1;
}

}

// Returns the array with the state of the road at each unit of time
}

``````

### Community Solutions

Solution by SantiMenendez19:

``````codefunction cyberReindeer(road, time) {
let result = []
let timePassed = 0
let pos = 0
let aux = "."
while (time > 0) {
time--
timePassed++
pos++
}
}
return result
}

``````

Solution by Achalogy:

``````function cyberReindeer(road, time) {
let a = 0
let b = "."

for (let i = 1; i < time; i++) {
a++