aoc-2018-python

Advent of Code 2018 Solutions in Python
git clone https://git.sinitax.com/sinitax/aoc-2018-python
Log | Files | Refs | README | sfeed.txt

part1 (4565B)


      1--- Day 13: Mine Cart Madness ---
      2
      3A crop of this size requires significant logistics to transport produce, soil, fertilizer, and so
      4on. The Elves are very busy pushing things around in carts on some kind of rudimentary system of
      5tracks they've come up with.
      6
      7Seeing as how cart-and-track systems don't appear in recorded history for another 1000 years, the
      8Elves seem to be making this up as they go along. They haven't even figured out how to avoid
      9collisions yet.
     10
     11You map out the tracks (your puzzle input) and see where you can help.
     12
     13Tracks consist of straight paths (| and -), curves (/ and \), and intersections (+). Curves connect
     14exactly two perpendicular pieces of track; for example, this is a closed loop:
     15
     16/----\
     17|    |
     18|    |
     19\----/
     20
     21Intersections occur when two perpendicular paths cross. At an intersection, a cart is capable of
     22turning left, turning right, or continuing straight.  Here are two loops connected by two
     23intersections:
     24
     25/-----\
     26|     |
     27|  /--+--\
     28|  |  |  |
     29\--+--/  |
     30   |     |
     31   \-----/
     32
     33Several carts are also on the tracks. Carts always face either up (^), down (v), left (<), or right
     34(>). (On your initial map, the track under each cart is a straight path matching the direction the
     35cart is facing.)
     36
     37Each time a cart has the option to turn (by arriving at any intersection), it turns
     38left the first time, goes straight the second time, turns right the third time, and then repeats
     39those directions starting again with left the fourth time, straight the fifth time, and so on. This
     40process is independent of the particular intersection at which the cart has arrived - that is, the
     41cart has no per-intersection memory.
     42
     43Carts all move at the same speed; they take turns moving a single step at a time. They do this based
     44on their current location: carts on the top row move first (acting from left to right), then carts
     45on the second row move (again from left to right), then carts on the third row, and so on.  Once
     46each cart has moved one step, the process repeats; each of these loops is called a
     47tick.
     48
     49For example, suppose there are two carts on a straight track:
     50
     51|  |  |  |  |
     52v  |  |  |  |
     53|  v  v  |  |
     54|  |  |  v  X
     55|  |  ^  ^  |
     56^  ^  |  |  |
     57|  |  |  |  |
     58
     59First, the top cart moves. It is facing down (v), so it moves down one square.  Second, the bottom
     60cart moves.  It is facing up (^), so it moves up one square. Because all carts have moved, the first
     61tick ends.  Then, the process repeats, starting with the first cart.  The first cart moves down,
     62then the second cart moves up - right into the first cart, colliding with it! (The location of the
     63crash is marked with an X.) This ends the second and last tick.
     64Here is a longer example:
     65
     66/->-\        
     67|   |  /----\
     68| /-+--+-\  |
     69| | |  | v  |
     70\-+-/  \-+--/
     71  \------/   
     72
     73/-->\        
     74|   |  /----\
     75| /-+--+-\  |
     76| | |  | |  |
     77\-+-/  \->--/
     78  \------/   
     79
     80/---v        
     81|   |  /----\
     82| /-+--+-\  |
     83| | |  | |  |
     84\-+-/  \-+>-/
     85  \------/   
     86
     87/---\        
     88|   v  /----\
     89| /-+--+-\  |
     90| | |  | |  |
     91\-+-/  \-+->/
     92  \------/   
     93
     94/---\        
     95|   |  /----\
     96| /->--+-\  |
     97| | |  | |  |
     98\-+-/  \-+--^
     99  \------/   
    100
    101/---\        
    102|   |  /----\
    103| /-+>-+-\  |
    104| | |  | |  ^
    105\-+-/  \-+--/
    106  \------/   
    107
    108/---\        
    109|   |  /----\
    110| /-+->+-\  ^
    111| | |  | |  |
    112\-+-/  \-+--/
    113  \------/   
    114
    115/---\        
    116|   |  /----<
    117| /-+-->-\  |
    118| | |  | |  |
    119\-+-/  \-+--/
    120  \------/   
    121
    122/---\        
    123|   |  /---<\
    124| /-+--+>\  |
    125| | |  | |  |
    126\-+-/  \-+--/
    127  \------/   
    128
    129/---\        
    130|   |  /--<-\
    131| /-+--+-v  |
    132| | |  | |  |
    133\-+-/  \-+--/
    134  \------/   
    135
    136/---\        
    137|   |  /-<--\
    138| /-+--+-\  |
    139| | |  | v  |
    140\-+-/  \-+--/
    141  \------/   
    142
    143/---\        
    144|   |  /<---\
    145| /-+--+-\  |
    146| | |  | |  |
    147\-+-/  \-<--/
    148  \------/   
    149
    150/---\        
    151|   |  v----\
    152| /-+--+-\  |
    153| | |  | |  |
    154\-+-/  \<+--/
    155  \------/   
    156
    157/---\        
    158|   |  /----\
    159| /-+--v-\  |
    160| | |  | |  |
    161\-+-/  ^-+--/
    162  \------/   
    163
    164/---\        
    165|   |  /----\
    166| /-+--+-\  |
    167| | |  X |  |
    168\-+-/  \-+--/
    169  \------/   
    170
    171After following their respective paths for a while, the carts eventually crash.  To help prevent
    172crashes, you'd like to know the location of the first crash. Locations are given in X,Y coordinates,
    173where the furthest left column is X=0 and the furthest top row is Y=0:
    174
    175           111
    176 0123456789012
    1770/---\        
    1781|   |  /----\
    1792| /-+--+-\  |
    1803| | |  X |  |
    1814\-+-/  \-+--/
    1825  \------/   
    183
    184In this example, the location of the first crash is 7,3.
    185
    186
    187
    188