Part 1
In this puzzle, we have a grid of cart tracks and carts moving on them. The carts start in a direction and will change their direction when the encounter a curve ("\" or "\") character. On intersections ("+") the cart can chose their direction. They will alternate between turning left, going straight and turning right. At each timestep the carts move one step accoring to their direction. We should simulate the carts until their first crash by two carts being in the same position.
We use a complex number to save the position and direction for each cart. Additionally we have to keep track of the number of intersections the carts has crossed so far for the intersection logic. For the simulation we had to make sure the carts are running in the correct order by sorting the carts each round. We can then update the positions by adding the current direction. We then have to check the current field and update the direction. Turning left and right is done by multiplying with i and -i respectively. Once we have two carts at the same position, we can output their position as the answer.
Part 2
In part 2 whenever carts crash, we should remove the two carts from the grid. This is repeated until there is only one cart remaining. We should return the position of this cart.
We added a "dead" field to our carts. Whenever there is a crash, we set both carts to "dead" and remove them from the list of carts later on. Once there is only cart remaining, we have our answer.
No comments:
Post a Comment