Mine Cart Madness


Fork me on GitHub
2018-12-13

Day 13: Mine Cart Madness

Description:
--- Day 13: Mine Cart Madness ---

A crop of this size requires significant logistics to transport produce, soil, fertilizer, and so on. The Elves are very busy pushing things around in carts on some kind of rudimentary system of tracks they've come up with.

Seeing as how cart-and-track systems don't appear in recorded history for another 1000 years, the Elves seem to be making this up as they go along. They haven't even figured out how to avoid collisions yet.

You map out the tracks (your puzzle input) and see where you can help.

Tracks consist of straight paths (| and -), curves (/ and \), and intersections (+). Curves connect exactly two perpendicular pieces of track; for example, this is a closed loop:

/----\
| |
| |
\----/

Intersections occur when two perpendicular paths cross. At an intersection, a cart is capable of turning left, turning right, or continuing straight. Here are two loops connected by two intersections:

/-----\
| |
| /--+--\
| | | |
\--+--/ |
| |
\-----/

Several carts are also on the tracks. Carts always face either up (^), down (v), left (<), or right (>). (On your initial map, the track under each cart is a straight path matching the direction the cart is facing.)

Each time a cart has the option to turn (by arriving at any intersection), it turns left the first time, goes straight the second time, turns right the third time, and then repeats those directions starting again with left the fourth time, straight the fifth time, and so on. This process is independent of the particular intersection at which the cart has arrived - that is, the cart has no per-intersection memory.

Carts all move at the same speed; they take turns moving a single step at a time. They do this based on their current location: carts on the top row move first (acting from left to right), then carts on the second row move (again from left to right), then carts on the third row, and so on. Once each cart has moved one step, the process repeats; each of these loops is called a tick.

For example, suppose there are two carts on a straight track:

| | | | |
v | | | |
| v v | |
| | | v X
| | ^ ^ |
^ ^ | | |
| | | | |

First, the top cart moves. It is facing down (v), so it moves down one square. Second, the bottom cart moves. It is facing up (^), so it moves up one square. Because all carts have moved, the first tick ends. Then, the process repeats, starting with the first cart. The first cart moves down, then the second cart moves up - right into the first cart, colliding with it! (The location of the crash is marked with an X.) This ends the second and last tick.

Here is a longer example:

/->-\
| | /----\
| /-+--+-\ |
| | | | v |
\-+-/ \-+--/
\------/

/-->\
| | /----\
| /-+--+-\ |
| | | | | |
\-+-/ \->--/
\------/

/---v
| | /----\
| /-+--+-\ |
| | | | | |
\-+-/ \-+>-/
\------/

/---\
| v /----\
| /-+--+-\ |
| | | | | |
\-+-/ \-+->/
\------/

/---\
| | /----\
| /->--+-\ |
| | | | | |
\-+-/ \-+--^
\------/

/---\
| | /----\
| /-+>-+-\ |
| | | | | ^
\-+-/ \-+--/
\------/

/---\
| | /----\
| /-+->+-\ ^
| | | | | |
\-+-/ \-+--/
\------/

/---\
| | /----<
| /-+-->-\ |
| | | | | |
\-+-/ \-+--/
\------/

/---\
| | /---<\
| /-+--+>\ |
| | | | | |
\-+-/ \-+--/
\------/

/---\
| | /--<-\
| /-+--+-v |
| | | | | |
\-+-/ \-+--/
\------/

/---\
| | /-<--\
| /-+--+-\ |
| | | | v |
\-+-/ \-+--/
\------/

/---\
| | /<---\
| /-+--+-\ |
| | | | | |
\-+-/ \-<--/
\------/

/---\
| | v----\
| /-+--+-\ |
| | | | | |
\-+-/ \<+--/
\------/

/---\
| | /----\
| /-+--v-\ |
| | | | | |
\-+-/ ^-+--/
\------/

/---\
| | /----\
| /-+--+-\ |
| | | X | |
\-+-/ \-+--/
\------/

After following their respective paths for a while, the carts eventually crash. To help prevent crashes, you'd like to know the location of the first crash. Locations are given in X,Y coordinates, where the furthest left column is X=0 and the furthest top row is Y=0:

111
0123456789012
0/---\
1| | /----\
2| /-+--+-\ |
3| | | X | |
4\-+-/ \-+--/
5 \------/

In this example, the location of the first crash is 7,3.

--- Part Two ---

There isn't much you can do to prevent crashes in this ridiculous system. However, by predicting the crashes, the Elves know where to be in advance and instantly remove the two crashing carts the moment any crash occurs.

They can proceed like this for a while, but eventually, they're going to run out of carts. It could be useful to figure out where the last cart that hasn't crashed will end up.

For example:

/>-<\
| |
| /<+-\
| | | v
\>+</ |
| ^
\<->/

/---\
| |
| v-+-\
| | | |
\-+-/ |
| |
^---^

/---\
| |
| /-+-\
| v | |
\-+-/ |
^ ^
\---/

/---\
| |
| /-+-\
| | | |
\-+-/ ^
| |
\---/

After four very expensive crashes, a tick ends with only one cart remaining; its final location is 6,4.

What is the location of the last cart at the end of the first tick where it is the only cart left?

Input:
/---------------------\ /----------------<---------------------------------------------------------------------\
| | | /---------------------------\ |
/-------+---------------------+-----------+---------+---------------------------+------\ |
/----------+-------+---------------------+---\ /+---------+---------------------------+------+------\ |
/-+----------+-------+---------------------+---+------++---------+-------------------------\ | | | |
| | | | /------------------+---+------++---------+-----\ | | /----+------+---------------------------\ |
| | /------+-------+--+------------------+---+-\ || | | /----+-+-+----+------+----------------\ | |
| | | | /-----+--+------------------+---+-+----++---------+-----+--------------+----+-+-+----+---\ | | | |
| | | | | /+--+--------------\ | | | || /----+-----+--------------+----+-+-+----+---+--+----------------+---------\| |
| | | | | || | | | | | || | | | | | | | | | | | || |
| | | | | || | /-------+---+-\ | | || | | | | | | | | | | /-------+---------++------+---\
| | | | | || | | | | | | | || | | | /--------+----+-+-+----+---+\ | | | || | |
| | | | | || | | | | | | | || |/---+-----+\ | | | | | | || | | | || | |
| | | /-+-+----++--+------+-------+---+-+-+-+----++----++---+-----++--->+--------+----+-+-+----+---++-+-----\ | | || | |
| | | | | | /++--+------+-------+---+-+-+-+----++----++---+-----++----+--------+----+-+-+----+---++-+-----+--+-------+-\ || | |
| | /+----+-+-+---+++--+------+-------+---+-+-+-+----++-\ || | || | | | | | | || | | | | | || | |
| | || | | | ||| | | | | | | | || | /++---+-----++----+--------+----+-+-+-\ | || | | | | | || | |
| | || |/+-+---+++--+------+-------+---+-+-+-+----++-+-+++---+\ /-++----+--\ | | | | | | || | | | | |/------++--\ | |
| | || ||| |/--+++--+------+-------+---+-+-+-+----++\| ||| || | || | | /---+----+-+-+-+--+---++-+----\| | | || || | | |
| | || ||| || ||| |/-----+-------+---+-+-+-+----++++-+++---++--+-++----+--+-+---+----+-+-+-+--+---++-+----++--+-------+-++---\ || | | |
| | || ||| || |\+--++-----+-------/ | | | | |||| ||| v| | || | | | | | | | | |/--++-+----++--+-------+-++---+--++--+---+--\|
/+-+--++----+++-++--+-+--++-----+-----------+-+-+-+----++++-+++---++--+-++----+--+-+-\ \----+-+-+-+--++--++-+----++--+-------/ || | || | | ||
|| | /++----+++-++--+-+--++-----+-----------+-+-+-+----++++-+++---++--+-++---\| | | | | | | | || || | || | || | || | | ||
|| | ||| ||| || | | || | | | | |/---++++-+++---++--+-++---++--+-+-+------+-+-+-+--++--++-+----++--+---------++--\| || | | ||
|| | |||/---+++-++--+-+--++-----+-----------+-+-+-++---++++-+++---++--+-++---++--+-+-+---\ | | | | || || | || | || || || | | ||
|| | |||| ||| || | | || | | | | || |||| ||| || | ||/--++--+-+-+---+--+-+\| | || || | || | || || || | | ||
|| | |||| ||| || /+-+--++-----+-----------+-+-+-++---++++-+++---++--+-+++--++--+-+-+---+--+-+++-+--++--++-+----++--+-------\ || || || | | ||
|| | |||| ||| \+-++-+--++-----+-----------+-+-+-++---++++-+++---++--+-+++--++--+-+-+---+--+-+++-+--++--/| | || \-------+-++--++--++--+---+--+/
|| | |||| ||| |/++-+--++-----+-----------+-+-+-++---++++-+++---++--+-+++\ || | | | | | ||| | || | | || | || || || | | |
|| | |||| ||| |||| | || | | | | || |||| ||| || | |||| || | | | | | ||| | || | | || | || || || | | |
|| |/++++---+++--++++-+--++---\ | | | | || |||| ||| ||/-+-++++-++--+-+-+---+--+-+++-+--++---+-+----++----------+-++--++--++--+---+--+\
|| |||||| ||| |||| | || | | | | | || |||| |\+---+++-+-++++-++--+-+-+---+--+-+++-+--++---+-+----++----------+-++--++--/| | | ||
|| |||||| ||| |||| | || | | /--------+-+-+-++---++++-+-+---+++-+-++++-++--+-+-+---+-\| ||| | || | | || | || || | | | ||
|| |||||| ||| |||| | /++---+-+--+--------+-+-+-++---++++-+-+---+++-+-++++-++--+-+-+---+-++-+++-+--++-\ | | || /--------+-++--++---+--+\ | ||
|| |||||| ||| |||| | ||| | | | /----+-+-+-++---++++-+-+---+++-+-++++-++--+-+-+---+-++-+++-+--++-+-+-+----++-+--------+-++--++---+--++--+-\||
|| |||||| ||| |||| | ||| | | | | | | | || /++++-+-+--\||| | |||| || |/+-+---+-++-+++-+--++-+-+-+----++-+--------+-++--++---+--++-\| |||
|| |||||| ||| |||| | ||| /+-+--+---+----+-+-+-++--+++++-+-+--++++-+-++++-++--+++-+---+-++-+++-+--++-+-+-+----++-+--\ | || || | || || |||
|| |||||| /-+++--++++-+-+++--++-+--+---+----+-+\| || ||||| | | |||| | |||| || ||| | | || ||| | || | | | || | | /-+-++--++---+\ || || |||
|| |||||| | ||| |||| \-+++--++-+--+---+----/ ||| || ||||| | | |||| | |||| || ||| | | || ||| | || | | | || | | | | || || || || || |||
|| ||||\+-+-+++--++++---+++--++-+--+---+>-----+++-/| ||||| | | |||\-+-++++-++--+++-+---+-++-+++-+--++-+-+-+----++-+--+---+-+-++--++---++-++-++-++/
|| |||| | | ||| |||| ||| || | | |/-----+++--+--+++++-+-+--+++--+-++++-++--+++-+---+-++-+++-+--++-+-+-+-\ || | | | | || || || || || ||
|| |||| | | ||| |\++---+++--++-+--+---++-----+++--+--+++++-+-+--+++--+-+++//++--+++-+---+-++-+++-+--++-+-+-+-+--++-+--+---+\| || || || || || ||
|| |||| | | ||| | |\---+++--++-+--+---++-----+++--+--+++++-+-+--+++--+>+++-+++--+++-+---+-++-+++-+--++-+-+-+-+--++-+--+---+++-/| || || || || ||
|| |||| | | ||| | | ||| || | | /-++-----+++--+--+++++-+-+--+++--+-+++-+++--+++-+---+-++-+++-+--++-+-+-+-+--++-+--+---+++--+--++-<-++-++-++\||
|| |||\-+-+-+++--+-+----+++--++-+--+-+-++-----+++--+--++++/ | | ||| | ||| ||| ||| | | || ||| | || |/+-+-+--++-+--+---+++--+--++---++-++-+++++\
|| ||| | | ||| | | ||| || | | | || ||| | |||| \-+--+++--+-+++-+++--+++-+---+-++-+++-/ || ||| | | || | | ||| | || || || ||||||
|| ||| | | ||| | | ||| || | | | ||/----+++--+--++++----+\ ||| | ||| ||| |||/+---+-++-+++----++-+++-+-+--++-+--+---+++--+--++--\|| || ||||||
|| ||| | | ||| | | ||| || | |/+-+++----+++--+--++++----++-+++--+-+++-+++--+++++---+-++-+++----++-+++-+-+--++-+--+---+++-\| || ||| || ||||||
|| \++--+-+-+++--+-+----+++--++-+--+++-+++----++/ | |||| || ||| \-+++-+++--/|||| | || ||| || ||| | | || | | ||| || || ||| || ||||||
|| || | | ||| | | ||| || | ||| ||| || | |||| || ||| ||| ||\---++++---+-++-+++----++-++/ | | || | | ||| || || ||| || ||||||
|| || | | ||| | | ||| || | ||| ||| || | |||| \+-+++----+/| \+----++++---+-++-+++----++-++--+-+--++-+--+---+/| || || ||| || ||||||
|| || /+-+-+++--+-+----+++--++-+--+++-+++----++---+--++++-----+-+++----+-+--+----++++---+\|| ||| || || | | || \--+---+-+-++--++--+++-+/ ||||||
|| || || | ||| | | /+++--++-+--+++-+++----++---+--++++-----+-+++----+-+--+----++++---++++-+++----++-++--+-+--++--\ | | | || || ||| | ||||||
|| || || | ||| | | |||| || | ||| ||| || | |||| | |\+----+-+--+----++++---++++-/|| || || | | || | | | | || || ||| | ||||||
|| || || | ||| | | /++++--++-+--+++-+++----++---+--++++-----+-+-+----+-+--+----++++---++++--++----++-++--+-+--++--+-+---+-+-++-\|| ||| | ||||||
|| || || | ||| | | ||||| || | ||| ||| || | |||| | | | | | | |||| |||| /++----++-++--+-+--++--+\| |/+-++-+++\ ||| | ||||||
|| || || | ||| | | ||||| || | ||| ||| || |/-++++-----+-+-+----+-+--+----++++---++++-+++\ || || | | || ||| ||| || |||| ||| | ||||||
|| || || | ||| | | /+++++--++-+--+++<+++----++---++-++++-----+-+-+----+-+--+----++++---++++-++++---++-++--+\| || ||| ||| || |||| ||| | ||||||
|| || || | ||| | | |||||| || | |\+-+++----++---++-++++-----+-+-+----+-+--+----++++---++++-++++---++-++--+++--++--+++---+++-/| |||| ||| | ||||||
|| || || | ||| | | |||\++--++-+--+-+-+++----++---++-++++-----+-+-+----+-+--+----++++---++++-++++---++-/| ||| || ||| ||| | |||| ||| | |^||||
|| || || | ||| | |/+++-++--++-+--+-+-+++----++---++-++++-----+-+-+----+-+--+----++++\ |||| |||| |\--+--+++--++--+++---+++--+-++++-+++-+--++++/|
|| || || | ||| | ||||| || || | | | ||| || || |||| | | | | | | ||\++--++++-++++---+---+--+++--++--+++---+++--+-++++-/|| | |||| |
|| || || | ||| | ||||| || || | | | ||| || || |||| | | | | | | || || |||| |||| | | ||| || ||| ||| | |||| || | |||| |
|| || || | \++--+-+++++-++--++-+--+-+-+++----++---++-++++-----+-+-+----+-+--+----++-++--++++-++++---+---+--+++--+/ ||| ||| | |||| || | |||| |
|| || || | || | ||||| || || | | | ||| || || |||| | | | | | | || || |||| |||| | | ||| | ||| ||| | |||| || | |||| |
|| ||/++-+--++--+-+++++-++--++-+--+-+-+++----++---++-++++-----+-+-+----+-+--+----++-++\ |||| |||| | | ||| | ||| ||| | |||| || | |||| |
|| \++++-+--++--+-+++++-++--+/ | | | ||| /--++---++-++++-----+-+-+----+-+--+----++-+++-++++-++++>--+---+--+++--+---+++---+++--+\|||| || | |||| |
|| |||| | || | ||||| |\--+--+--+-+-+++-+--++---++-++++-----+-+-+----+-+--+----++-+++-++++-++++---+---+--+++--+---+++---+++--++++/| || | |||| |
|| |||| | || | ||||| | | | | | ||\-+--++---++-++++-----/ | | |/+--+----++-+++\|||| |||| |/--+--+++--+---+++---+++--++++-+--++-+-\|||| |
|| |||| | || | ||||| | | | | | || |/-++---++-++++-------+-+----+++--+----++-++++++++-++++---++--+--+++--+---+++---+++--++++-+--++\| ||||| |
|| |||| | || | ||||| | /-+--+--+-+-++--++-++---++-++++-------+-+----+++--+----++-++++++++-++++---++--+--+++\ | ||| ||| |||| | |||| ||||| |
|\---++++-+--++--+-+++++-+-+-+--+--+-+-++--++-++---++-++++-------+-+----+++--+----++-+++++++/ |||| || | |||| | ||| ||| |||| | |||| ||||| |
| |||\-+--++--+-+++++-+-+-+--+--+-+-++--++-++---++-++++-------+-+----+++--+----++-++++/|| |||| || | |||| | ||| ||| |||| | |||| ||||| |
| |||/-+--++--+-+++++-+-+-+--+--+-+-++--++-++---++-++++-------+-+-\ ||| | || |||| || |||| || | |||| | ||| ||| |||| | |||| ||||| |
| |||| | || | ||||| | | | | | | || || || |\-++++-------+-+-+--+++--+----++-++++-++--+++/ || | |||| | ||| ||| \+++-+--+++/ ||||| |
| |||| | || | ||||| | | |/-+--+-+-++--++-++---+--++++-------+-+-+--+++--+----++-++++-++--+++----++--+--++++\| ||| ||| ||| | ||| ||||| |
| |||| | || | ||||| | | || | | | || || || /-+--++++-------+-+-+--+++--+--\ || |||| || ||| || | |||||| ||| ||| ||| | ||| ||||| |
| |||| | || | ||||| \-+-++-+--+-+-++--++-++-+-+--++++-------+-+-+--/|| | | || |||| || ||| || | |||||| ||| ||| ||| | ||| ||||| |
| |||| | || | ||||| | || | | | || || || | | |||| /-----+-+-+---++--+--+-++-++++-++--+++----++--+--++++++---+++---+++\ ||| | ||| ||||| |
| |||| | \+--+-+++++---+-++-+--+-+-++--++-++-+-+--++++-+-----+-/ | || | | || |||| || ||| /++--+--++++++\ ||| |||| /+++-+--+++\ ||||| |
| |||| \---+--+-+++++---+-++-+--+-+-++--++-+/ v | |\++-+-----+---+---++--+--+-++-++++-++--+++---+++--+--/|||||| ||| |||| |||| | |||| ||||| |
| |||| /--+--+-+++++--\|/++-+--+-+-++--++-+--+-+--+-++-+-----+---+---++--+--+-++-++++-++--+++---+++--+\ |||||| ||| |||| |||| | |||| ||||| |
| |||| | | /+-+++++--+++++-+--+-+-++--++-+--+-+--+-++-+-----+---+---++-\| | || |||| || ||| |||/-++--++++++-\||| |||| |||| | |||| ||||| |
| |||| | | || ||||| ||||| | | | || || | | | | || |/----+---+---++-++--+-++-++++-++--+++---++++-++--++++++-++++\ |||| |||| | |||| ||||| |
\----++++--+--+-++-+++++--+++++-+--+-+-++--++-+--+-+--+-++-++----+---+---++-++--+-++-/||| || ||| |||| || |||||| ||||| |||| |||| | |||| ||||| |
/++++--+--+-++-+++++\ |||\+-+--+-+-++--++-+--+-+--+-++-++----+---+---++-++--+-++--+++-++--+++---++++-++--++++++-+++/| |||| |||| | |||| ||||| |
|||||/-+--+-++-++++++-+++-+-+-\| \-++--++-+--+-+--+-++-++----+---+---++-++--+-++--+++-++--+++---++++-++--++++++-+++-+--++++-++++-+--++++-+++/| |
|||||| | | || |||||| |\+-+<+-++---++--++-+--+-+--+-++-++----+---+---++-++--+-++--+++-++--+++---++++-++--++/||| ||| | |||| |||| | |||| ||| | |
|||||| | | || |||||| | | \-+-++---++--++-+--+-+--+-++-++----+---+---++-++--+-++--+++-++--+++---++++-++--++-/|| ||| | |||| |||| | |||| ||| | |
|||||| | | || |||||| | | | || || || | \-+--+-++-++----+---+---++-++--/ || ||| || ||| |||| || || || ||| | |||| |||| | |||| ||| | |
|||||| | | || |||\++-+-+---+-++---++--++-+----+--+-++-++----+---+---++-++----++--+++-++--+++---++++-++--++--++-+++-+--++++-++/| | |||| ||| | |
|||||| | |/++-+++-++-+-+---+-++---++--++-+----+--+-++-++----+---+---++-++----++--+++-++\ ||| |||| || || || ||| | |||| || | | |||| ||| | |
|||||| | |||| ||| || | | | |\---++--++-+----+--+-++-++----+---+---++-++----++--+++-+/| ||| |||| || || || ||| | |||| || | | |||| ||| | |
|||||| | |||| ||| || | | | | || \+-+----+--+-++-++----+---+---++-++----++--+++-+-+-+++---++++-++--++--++-+++-+--++++-+/ | | |||| ||| | |
|||||| | |||| ||| || | | | | ||/--+-+----+--+-++-++---\| | || || || ||| | | ||| |||| || || || ||| | |||| | | | |||| ||| | |
|||||| | |||| ||| || | | | | ||| | | | | || || || | || || || ||| | | ||\---++++-++--++--++-+++-+--++++-+--+-+--/||| ||| | |
|||||| | |||| ||| |^ | | | | ||| | | | | || || || | || || || |||/+-+-++----++++-++--++--++-+++\| |||| | | | ||| ||| | |
/-++++++-+--++++\||| || | | | | ||| | | | | || || || | \+-++----++-<++/|| | || |||| || || || ||||| |||| | | | ||| ||| | |
| |||||| \--++++++++-++-/ | | | ||| /+-+----+--+-++-++---++---+---\| || /--++--++-++-+-++----++++-++--++--++-+++++--++++-+--+\| ||| ||| | |
| |||||| |||||||| || | | | ||| || | | | || || || | || || | || || || | \+----++++-++--++--++-++/|| |||| | ||| ||| ||| | |
| |||||| |||||||| || | | | /--+++-++-+----+--+-++-++---++---+\ || || | \+--++-++-+--+----++++-++--++--++-++-++--++++-+--+++---+++-+/| | |
| |||||| |||||||| || | | | | ||| || | | /+-++-++---++---++--++-++-+---+--++-++-+--+----++++-++--++--++-++-++--++++-+--+++---+++-+-+-+-+\
| |||||| |||||||| || | | | | ||| || | | || \+-++---++---++--++-++-+---+--++-++-+--+----++++-++--++--++-++-++--++++-+--+++---+++-+-/ | ||
| |||||| \+++++++-++---+---+-+-+--+++-++-+----+-++--+-++---++---++--++-++-+---+--++-++-+--+----+/|| || || || || || |||| | ||| ||| | | ||
| |||||| ||||||| || | | | | ||| || | | || | || || || || || | | || || | | | || || || || || || |||| | ||| ||| | | ||
| |||||| ||||||| || | | | | \++-++-+----+-++--+-++---++---++--++-++-+---+--++-++-+--+----+-++-++--++--++-++-++--++++-+--+++---+++-+---/ ||
| \+++++-----+++++++-+/ | \-+-+---++-++-/ | || | || || || || || | \--++-++-+--+----+-++-++--++--/| || || |\++-+--++/ ||| | ||
| |v|||/----+++++++-+----+--\ | | || || \-++--+-++---++---++--++-++-+------++-++-+--+----+-++-++--++---+-++-++--+-++-+--/| ||| | ||
| |||||| \++++++-+----+--+--+-+---++-++--------++--+-++---++---++--++-++-+------++-++-/ | | || || || | || || | || | | ||| | ||
| |||||| /-++++++-+----+--+--+-+---++-++--------++--+-++---++---++--++-++-+------++-++----+----+-++\|| || | || || | || | | ||| | ||
| |||||| | |||||| | | | | | \+-++--------++--+-++---++---++--++-++-+------++-++----+----+-+++++--+/ | || || | || | | ||| | ||
| |||||| | |||||| \----+--+--+-+----+-++--------++--+-++---++---++--++-++-+------++-++----+----+-+++++--+----+-+/ || | || | | ||| | ||
\--++++++---+-++/||| | | | | | || || | || || || || || | /--++-++----+----+-+++++--+----+-+--++\ | || | | ||| | ||
|||||| | || ||| | | | | | \+--------++--+-++---++---++--/| || | | || || | | ||||| | | | ||| | || | | ||| | ||
\+++++---+-++-+++------+--+--+-+----+--+--------++--+-++---++---++---+-+/ \---+--++-++----+----+-+++++--+----+-+--+++-+-++-+---/ ||| | ||
||||| /-+-++-+++------+--+--+-+----+--+--------++--+-++---++---++---+-+------+--++-++----+----+-+++++--+\ | | ||| \-++-+--------/|| | ||
||||| | | || ||| \--+--+-+----+--+--------++--+-++---++---++---+-+->----+--++-++----+----+-++++/ || | | ||| || | || | ||
||||| | | || ||| | | | | | |\--+-++---+/ ||/--+-+------+--++-++----+---\| |\++---++---+-/ ||| || | || | ||
/----+++++-+-+-++\||| | | | \--+--------+---+-++---/ ||| | | | || || | || | |\---++---+----+++---++-+---------++-+-----/|
| ||||| | | |||||| | | | | | | || ||| | | | || || | || | | || | ||| || | || | |
| ||||| | | |\++++---------+--+-+-------+--------+---/ || ||| | | | || || | || | | || | ||| || | || | |
| ||||| | | | |||| | | | | | || /----+++--+-+------+--++-++----+---++-+-+----++---+----+++---++-+------\ || | |
| ||||| | | | |||| | | | | \-----++---+----+++--+-+------+--++-++----+---++-+-+----++---+----+++---++-+------+--++-+------/
| ||||| | | | |||| | | \-------+--------------++---+----+/| | | | || || | || | | || | ||| /-++-+------+-\|| |
| ||||| | | | |||| | | | \+---+----+-+--+-+------+--++-++----+---++-+-+----++---+----+++-+-+/ | | ||| |
| ||||| | \-+-++++---------+--+---------+---------------+---+----+-+--+-+------+--++-++----+---++-+-/ || | ||| | | | | ||| |
| ||||| | | |||\---------+--+---------+---------------+---+----+-+--+-+------+--++-++----+---++-+------/| | ||| | | | | ||| |
|/---+++++-+---+-+++-\ | | /---+---------------+---+----+-+--+-+------+--++-++----+---++-+-------+---+----+++-+-+--+------+\||| |
\+---+++++-+---+-/|| | | | | | |/--+----+-+--+-+------+--++-++----+---++-+-----\ | | ||| | | | ||||| |
| \++++-+---+--++-+--------+--+-----+---+---------------++--+----+-+--+-+------+--+/ || | || | | | | ||| | | \------++++/ |
| |||| | | || | | | | | || | | | \-+------+--+--++----/ || | | | | ||| | | |||| |
| |||| | | || | | | v | || | | | | | | \+--------++-+-----+-+---+----/|| | | |||| |
| ||\+-+---+--++-+--------+--/ | | || | | | | | | | |\-+-----+-+---/ || | | |||| |
| || | | | \+-+--------+--------+---+---------------++--+----+-+----+------+--+---+--------+--+----<+-+---------++-+-/ |||| |
| || | | | \-+--------+--------+---+---------------++--+----+-+----+------+--/ | | | | | || \-----------++/| |
| \+-+-+---+-----+--------+--------+---+---------------++--+----+-+----+------+------/ | | | | || || | |
| | | | | | | \---+---------------++--+----+-+----+------+---------------+--+-----+-+---------++-------------+/ | |
| | | | | | | | || | | | | | | | | | || | | |
| | | \---+-----+--------+------------+---------------++--+----+-+----+------+---------------+--+-----+-/ || | | |
| | | \-----+--------+------------+---------------++--+----+-+----/ | | | | || | | |
| | | | | | /----------++--+----+-+-----------+---------------+--+-----+-----------++----\ | | |
| | | | | | | \+--+----+-+-----------+---------------+--+-----+-----------/| | | | |
\-----+-+-----------/ | | | \--+----+-+-----------+---------------+--+-----/ | | | | |
| | | \----+--------------+----+-+-----------+---------------+--+------------------+----+--------+--/ |
| | | | | | | \---------------+--+------------------/ | | |
\-+--------------------+-----------------+--------------+----/ | | | | | |
| | | | \---------------------------/ | | | |
\--------------------/ | \-------------------------------------+-----------------------+--------/ |
| \-----------------------+--------------/
\----------------------------------------------------------------------------/

Part 1:
class Rail
{
    public int X,Y;
    public Rail N,E,S,W;
}
class Cart
{
    public Dir D;
    public Rail R;
	public int Mem=0;
}
enum Dir {N=0,E=1,S=2,W=3}

void Main()
{
	(List<Rail> rails, List<Cart> carts) = Parse(File.ReadAllLines(Path.Combine(Path.GetDirectoryName(Util.CurrentQueryPath), @"13_input.txt")));

	for (int gen=0;;gen++)
    {
        foreach	(var c in carts.OrderBy(c => c.R.Y*10000+c.R.X))
		{
			switch (c.D)
			{
				case Dir.S:
					Move(c, new[] { (Dir.E, c.R.E), (Dir.S, c.R.S), (Dir.W, c.R.W) });
					break;
				case Dir.W:
					Move(c, new[] { (Dir.S, c.R.S), (Dir.W, c.R.W), (Dir.N, c.R.N) });
					break;
				case Dir.N:
					Move(c, new[] { (Dir.W, c.R.W), (Dir.N, c.R.N), (Dir.E, c.R.E) });
					break;
				case Dir.E:
					Move(c, new[] { (Dir.N, c.R.N), (Dir.E, c.R.E),  (Dir.S, c.R.S) });
					break;
			}

			if (carts.Any(oc => oc != c && oc.R == c.R)) { $"{c.R.X},{c.R.Y} after {gen}".Dump(); return; }

		}
		//foreach (var c in carts) $"{c.R.X}|{c.R.Y}|{c.D}".Dump();"------".Dump();
	}
    
}

void Move(Cart c, IEnumerable<(Dir, Rail)> options)
{
	var outs = options.Where(p => p.Item2 != null).ToList();
	if (outs.Count == 1)
	{
		c.R = outs[0].Item2;
		c.D = outs[0].Item1;
	}
	else
	{
		c.R = outs[c.Mem % 3].Item2;
		c.D = outs[c.Mem % 3].Item1;
		c.Mem++;
	}

}

void Dump(List<Rail> rails, List<Cart> carts)
{
	int maxx = rails.Max(r => r.X) + 1;
	int maxy = rails.Max(r => r.Y) + 1;

	var b = new StringBuilder();
	for (int yy = 0; yy < maxy; yy++)
	{
		for (int xx = 0; xx < maxx; xx++)
		{
			var cc = carts.FirstOrDefault(c => c.R.X == xx && c.R.Y == yy);
			if (cc != null)
			{
				if (cc.D == Dir.N) b.Append('^');
				if (cc.D == Dir.E) b.Append('>');
				if (cc.D == Dir.S) b.Append('v');
				if (cc.D == Dir.W) b.Append('<');
			}
			else if (rails.Any(r => r.X == xx && r.Y == yy))
			{
				b.Append('.');
			}
			else
			{
				b.Append(' ');
			}
		}
		b.AppendLine();
	}
	b.ToString().Dump();
	"".Dump();
}

(List<Rail>, List<Cart>) Parse(string[] line)
{
    var rails = new Dictionary<(int,int), Rail>();
    var carts = new List<Cart>();

    Rail get(int x, int y) { if(rails.TryGetValue((x,y), out var v))return v; Rail r = new Rail { X = x, Y = y }; rails.Add((x,y),r); return r; }
    
    char last = ' ';
    for (int y = 0; y < line.Length; y++)
    {
        last = ' ';
        for (int x = 0; x < line[y].Length; x++)
        {
            char c = line[y][x];
            if (c==' ') continue;
            
            switch (c)
            {
                case '-':
                    {
                        var rr = get(x, y);
                        var ra = get(x - 1, y);
                        var rb = get(x + 1, y);
                        rr.W = ra; ra.E = rr;
                        rr.E = rb; rb.W = rr;
                    }
                    break;
                case '|':
                    {
                        var rr = get(x, y);
                        var ra = get(x, y - 1);
                        var rb = get(x, y + 1);
                        rr.N = ra; ra.S = rr;
                        rr.S = rb; rb.N = rr;
                    }
                    break;
                case '+':
                    {
                        var rr = get(x, y);
                        var ra = get(x, y - 1);
                        var rb = get(x + 1, y);
                        var rc = get(x, y + 1);
                        var rd = get(x - 1, y);
                        rr.N = ra; ra.S = rr;
                        rr.E = rb; rb.W = rr;
                        rr.S = rc; rc.N = rr;
                        rr.W = rd; rd.E = rr;
                    }
                    break;
                case '/':
                    if (last == '-' || last == '+')
                    {
                        var rr = get(x, y);
                        var ra = get(x - 1, y);
                        var rb = get(x, y - 1);
                        rr.W = ra; ra.E = rr;
                        rr.N = rb; rb.S = rr;
                    }
                    else
                    {
                        var rr = get(x, y);
                        var ra = get(x + 1, y);
                        var rb = get(x, y + 1);
                        rr.E = ra; ra.W = rr;
                        rr.S = rb; rb.N = rr;
                    }
                    break;
                case '\\':
                    if (last == '-' || last == '+')
                    {
                        var rr = get(x, y);
                        var ra = get(x - 1, y);
                        var rb = get(x, y + 1);
                        rr.W = ra; ra.E = rr;
                        rr.S = rb; rb.N = rr;
                    }
                    else
                    {
                        var rr = get(x, y);
                        var ra = get(x + 1, y);
                        var rb = get(x, y - 1);
                        rr.E = ra; ra.W = rr;
                        rr.N = rb; rb.S = rr;
                    }
                    break;
                case '^':
                    {
                        var rr = get(x, y);
                        var ra = get(x, y - 1);
                        var rb = get(x, y + 1);
                        rr.N = ra; ra.S = rr;
                        rr.S = rb; rb.N = rr;
                        carts.Add(new Cart { D = Dir.N, R=rr, });
                    }
                    break;
                case 'v':
                    {
                        var rr = get(x, y);
                        var ra = get(x, y - 1);
                        var rb = get(x, y + 1);
                        rr.N = ra; ra.S = rr;
                        rr.S = rb; rb.N = rr;
                        carts.Add(new Cart { D = Dir.S, R=rr, });
                    }
                    break;
                case '>':
                    {
                        var rr = get(x, y);
                        var ra = get(x - 1, y);
                        var rb = get(x + 1, y);
                        rr.W = ra; ra.E = rr;
                        rr.E = rb; rb.W = rr;
                        carts.Add(new Cart { D = Dir.E, R = rr, });
                    }
                    break;
                case '<':
                    {
                        var rr = get(x, y);
                        var ra = get(x - 1, y);
                        var rb = get(x + 1, y);
                        rr.W = ra; ra.E = rr;
                        rr.E = rb; rb.W = rr;
                        carts.Add(new Cart { D = Dir.W, R = rr, });
                    }
                    break;
            }
            last = c;
        }
    }
    
    return (rails.Select(p=>p.Value).ToList(), carts);
}
Result: 124,90

Part 2:
class Rail
{
    public int X,Y;
    public Rail N,E,S,W;
}
class Cart
{
	public bool C=false;
	public Dir D;
	public Rail R;
	public int Mem = 0;
}
enum Dir { N = 0, E = 1, S = 2, W = 3 }

void Main()
{
	(List<Rail> rails, List<Cart> carts) = Parse(File.ReadAllLines(Path.Combine(Path.GetDirectoryName(Util.CurrentQueryPath), @"13_input.txt")));

	int cc = carts.Count;
	for (int gen = 0; ; gen++)
	{
		foreach (var c in carts.OrderBy(c => c.R.Y * 10000 + c.R.X))
		{
			if (c.C)continue;
			switch (c.D)
			{
				case Dir.S:
					Move(c, new[] { (Dir.E, c.R.E), (Dir.S, c.R.S), (Dir.W, c.R.W) });
					break;
				case Dir.W:
					Move(c, new[] { (Dir.S, c.R.S), (Dir.W, c.R.W), (Dir.N, c.R.N) });
					break;
				case Dir.N:
					Move(c, new[] { (Dir.W, c.R.W), (Dir.N, c.R.N), (Dir.E, c.R.E) });
					break;
				case Dir.E:
					Move(c, new[] { (Dir.N, c.R.N), (Dir.E, c.R.E), (Dir.S, c.R.S) });
					break;
			}

			var dc = carts.FirstOrDefault(oc => oc != c && oc.R == c.R && !oc.C);
			if (dc != null) { c.C = dc.C = true; cc-=2; }
			if (cc == 1) { $"{carts.Single(p => !p.C).R.X},{carts.Single(p => !p.C).R.Y} after {gen}".Dump(); return; }

		}
		//foreach (var c in carts) $"{c.R.X}|{c.R.Y}|{c.D}".Dump();"------".Dump();
	}

}

void Move(Cart c, IEnumerable<(Dir, Rail)> options)
{
	var outs = options.Where(p => p.Item2 != null).ToList();
	if (outs.Count == 1)
	{
		c.R = outs[0].Item2;
		c.D = outs[0].Item1;
	}
	else
	{
		c.R = outs[c.Mem % 3].Item2;
		c.D = outs[c.Mem % 3].Item1;
		c.Mem++;
	}

}

void Dump(List<Rail> rails, List<Cart> carts)
{
	int maxx = rails.Max(r => r.X) + 1;
	int maxy = rails.Max(r => r.Y) + 1;

	var b = new StringBuilder();
	for (int yy = 0; yy < maxy; yy++)
	{
		for (int xx = 0; xx < maxx; xx++)
		{
			var cc = carts.FirstOrDefault(c => c.R.X == xx && c.R.Y == yy);
			if (cc != null)
			{
				if (cc.D == Dir.N) b.Append('^');
				if (cc.D == Dir.E) b.Append('>');
				if (cc.D == Dir.S) b.Append('v');
				if (cc.D == Dir.W) b.Append('<');
			}
			else if (rails.Any(r => r.X == xx && r.Y == yy))
			{
				b.Append('.');
			}
			else
			{
				b.Append(' ');
			}
		}
		b.AppendLine();
	}
	b.ToString().Dump();
	"".Dump();
}

(List<Rail>, List<Cart>) Parse(string[] line)
{
	var rails = new Dictionary<(int, int), Rail>();
	var carts = new List<Cart>();

	Rail get(int x, int y) { if (rails.TryGetValue((x, y), out var v)) return v; Rail r = new Rail { X = x, Y = y }; rails.Add((x, y), r); return r; }

	char last = ' ';
	for (int y = 0; y < line.Length; y++)
	{
		last = ' ';
		for (int x = 0; x < line[y].Length; x++)
		{
			char c = line[y][x];
			if (c == ' ') continue;

			switch (c)
			{
				case '-':
					{
						var rr = get(x, y);
						var ra = get(x - 1, y);
						var rb = get(x + 1, y);
						rr.W = ra; ra.E = rr;
						rr.E = rb; rb.W = rr;
					}
					break;
				case '|':
					{
						var rr = get(x, y);
						var ra = get(x, y - 1);
						var rb = get(x, y + 1);
						rr.N = ra; ra.S = rr;
						rr.S = rb; rb.N = rr;
					}
					break;
				case '+':
					{
						var rr = get(x, y);
						var ra = get(x, y - 1);
						var rb = get(x + 1, y);
						var rc = get(x, y + 1);
						var rd = get(x - 1, y);
						rr.N = ra; ra.S = rr;
						rr.E = rb; rb.W = rr;
						rr.S = rc; rc.N = rr;
						rr.W = rd; rd.E = rr;
					}
					break;
				case '/':
					if (last == '-' || last == '+')
					{
						var rr = get(x, y);
						var ra = get(x - 1, y);
						var rb = get(x, y - 1);
						rr.W = ra; ra.E = rr;
						rr.N = rb; rb.S = rr;
					}
					else
					{
						var rr = get(x, y);
						var ra = get(x + 1, y);
						var rb = get(x, y + 1);
						rr.E = ra; ra.W = rr;
						rr.S = rb; rb.N = rr;
					}
					break;
				case '\\':
					if (last == '-' || last == '+')
					{
						var rr = get(x, y);
						var ra = get(x - 1, y);
						var rb = get(x, y + 1);
						rr.W = ra; ra.E = rr;
						rr.S = rb; rb.N = rr;
					}
					else
					{
						var rr = get(x, y);
						var ra = get(x + 1, y);
						var rb = get(x, y - 1);
						rr.E = ra; ra.W = rr;
						rr.N = rb; rb.S = rr;
					}
					break;
				case '^':
					{
						var rr = get(x, y);
						var ra = get(x, y - 1);
						var rb = get(x, y + 1);
						rr.N = ra; ra.S = rr;
						rr.S = rb; rb.N = rr;
						carts.Add(new Cart { D = Dir.N, R = rr, });
					}
					break;
				case 'v':
					{
						var rr = get(x, y);
						var ra = get(x, y - 1);
						var rb = get(x, y + 1);
						rr.N = ra; ra.S = rr;
						rr.S = rb; rb.N = rr;
						carts.Add(new Cart { D = Dir.S, R = rr, });
					}
					break;
				case '>':
					{
						var rr = get(x, y);
						var ra = get(x - 1, y);
						var rb = get(x + 1, y);
						rr.W = ra; ra.E = rr;
						rr.E = rb; rb.W = rr;
						carts.Add(new Cart { D = Dir.E, R = rr, });
					}
					break;
				case '<':
					{
						var rr = get(x, y);
						var ra = get(x - 1, y);
						var rb = get(x + 1, y);
						rr.W = ra; ra.E = rr;
						rr.E = rb; rb.W = rr;
						carts.Add(new Cart { D = Dir.W, R = rr, });
					}
					break;
			}
			last = c;
		}
	}

	return (rails.Select(p => p.Value).ToList(), carts);
}
Result: 145,88


made with vanilla PHP and MySQL, no frameworks, no bootstrap, no unnecessary* javascript