# String represents a road. One character travels on the road obeying the stops - Code challenge (advent.js day 5)

This is my code to solve the 5th Adventjs challenge. In that link you can read the instructions.

How can I improve it? It's a bit messy... and I'm repeating some part of the code.

• All must be in just one function
function cyberReindeer(road, time) {
// Code here
let greenLight = false;
let stop = false;

for (let i = 0; roadArr.length < time; i++) {
if(greenLight !== true) {
if (theRoad.charAt(i) !== "|" && stop !== true) {
.replace(/.$/,"S") + theRoad.slice(i+1, theRoad.length); roadArr.push(roadNow); } else { while(roadArr.length < 5 && roadArr.length < time) { roadArr.push(roadNow); stop = true; } theRoad = theRoad.replace(/\|/g, '*'); greenLight = true; roadNow = theRoad.slice(0, i + 1) .replace("S", ".") .replace(/.$/,"S")
}
}
} else {
.replace("S", ".")
.replace(/.\$/,"S")
}
}
}


The original problem asks us to

Create a function that simulates the sled's movement ...

Somehow you leapt from that to

All must be in just one function.

I see no prohibition on adding a helper function, if you find it convenient.

# painting a sprite

Santa is a spritely old elf.

Three times you use an odd idiom for erasing his sled and then re-drawing it.

It would be simpler to just copy the original road terrain to the result each time (thereby "erasing" Sanata), followed by placing Santa at his new location between a pair of substrings.

# velocity

Surprisingly, we're not modeling Santa's position with an x variable. The current solution resembles computation by cellular automata.

Having adopted that we might have a per time-step velocity delta_x, so the update for each cycle is x += delta_x. Setting that delta to zero would be a natural way to model Santa stopping briefly for a Red light.

# consistent tests

I was a little surprised to see comparisons to 5 or to time like this: roadArr.length < time. It would have been more natural, at time-step i, to compare against the loop variable: i < time. Due to how the array grows, the effect is the same. Clarity could be improved.

# separation of concerns

This simulation of the world has two things to manage each time-step:

1. Stop lights ("barriers") sometimes switch from Red to Green
2. Santa moves, subject to Red light restrictions

It might be worth breaking out a pair of helper functions for that.

This simple world seems very physics based to me -- Santa performs local sensing of an adjacent barrier. Yet we have a global comparison to 5 which alters Santa's behavior.

I was hoping to see simpler logic where we change the world by replacing all | bars with * stars when i == 5, and then we let Santa move when there's no | bar. The stop and greenLight booleans seem a bit much, it's more things to juggle in your head than are necessary.

• Thank you. I'll take all into consideration and give it another try. When creating another function got this message: "Not allowed code outside the function. All your code MUST be inside cyberReindeer function" Commented Dec 5, 2023 at 22:48
• Wow, picky execution environment. Ok, one function it is.
– J_H
Commented Dec 5, 2023 at 22:52
• sharing another solution clearly got some insights and found very useful resources lately. thanks. Commented Dec 9, 2023 at 3:21