Browse Source

Completed day 13 part 1

Tyler Hallada 4 years ago
parent
commit
4b2a232f11
4 changed files with 501 additions and 3 deletions
  1. 150 0
      inputs/13.txt
  2. 6 0
      inputs/13_test.txt
  3. 338 0
      src/day13.rs
  4. 7 3
      src/main.rs

+ 150 - 0
inputs/13.txt

@@ -0,0 +1,150 @@
1
+                 /-----------------------------\                                             /-------------------------------\                        
2
+       /---------+-----------------------------+---------------------------------------------+--------------------\          |                        
3
+       |         |            /----------------+------------------------------------------\  |                    |          |                        
4
+       |         |            |                |                          /---------------+--+--------------\     |          |                        
5
+       |         |            |  /-------------+-------------\            |               |  |              |     |          |                        
6
+       |         |            |  |             |         /->-+------------+---------------+--+------------\ |     |          |                        
7
+   /---+---------+----------\ |  |             |         |   |   /--------+---------------+--+----------\ | |     |         /+-------------------\    
8
+   |   |         |          | |  |            /+---------+---+---+------<-+---------------+--+---------\| | |  /--+---------++---------------\   |    
9
+   |   |    /----+--------<-+-+--+------------++---------+---+---+--------+---------------+--+---------++-+-+--+--+-\       ||               |   |    
10
+   |   |    |    | /--------+-+--+---------\  ||         |   |   |        |     /---------+--+-\      /++-+-+--+-\| |       ||               |   |    
11
+/--+---+----+----+-+--------+-+--+---------+--++---------+---+---+--------+-----+---------+--+-+-----\||| | |  | || |       ||               |   |    
12
+|  |   |    |    | |    /---+-+--+---------+-\||      /--+---+---+--------+-----+---------+--+-+-----++++-+\|  | || |       ||               |   |    
13
+|  |   |   /+----+-+----+--\|/+--+---------+-+++------+\ |   |   |        |     |         |  | |     |||| |||  | || |       ||               |   |    
14
+|  |   |   ||    | |    |  ||||  |         | |||      || |   |   |        |     |      /--+--+-+-----++++-+++--+-++-+------\||               |   |    
15
+|  |   |   ||    | |    |  ||||  |    /----+-+++------++-+---+---+--------+-----+------+--+--+-+-----++++-+++--+-++-+------+++-----------\   |   |    
16
+|  |   |   ||    | |   /+--++++--+----+----+-+++-\    || |   |   |        |     |      |  |  | |     |||| |||  | || |      |||           |   |   |    
17
+|  |   |   ||    \-+---++--++++--+----+----+-++/ |    || |   |   |        |     |      |  |  | |     |||| |||  | || |      |||           |   |   |    
18
+|  |   |   ||      |   ||  ||||  |    |    | ||  | /--++-+---+---+--------+-----+------+\ |  | |     |||| |||  | || |      |||           |   |   |    
19
+|  |   |   ||      |   ||  ||||  |    |    | ||  | |  || |   |   |        \-----+------++-+--+-+-----++++-++/  | || |      |||           |   |   |    
20
+|  |   |   ||      |   ||  ||||  |    |    | ||  | |/-++-+---+---+--------------+------++-+--+-+-----++++-++---+\|| |      |||           |   |   |    
21
+|  |   |   ||      |   |\--++++--+----+----+-/|  | || || |   |   |              |      || |/-+-+-----++++-++---++++-+------+++-----------+---+---+---\
22
+|  |   |   ||      |   |   ||||/-+----+----+--+--+-++-++-+---+---+--------------+------++-++-+-+-----++++-++---++++-+---\  |||           |   |   |   |
23
+|  |   |   ||      |   |   ||||| |    |    |  |  | || || |   |   |              |      || || \-+-----++++-++---++++-+---+--++/           |   |   |   |
24
+|  \---+---++------+---+---+/||| | /--+----+--+--+-++-++\|   |   |         /----+------++-++---+-----++++-++---++++-+---+--++------------+---+\  |   |
25
+|     /+---++------+---+---+-+++-+-+--+--\ |  |  | || ||||   |   |  /------+----+-\    || ||   |     |||| ||   |||| |   |  ||  /---------+---++--+-\ |
26
+|     ||   ||      |   |   | \++-+-+--+--+-+--+--+-++-+/||/--+---+--+------+----+-+----++-++---+-----++++-++---++++-+---+--++--+---------+\  ||  | | |
27
+|     ||   ||      |   |   |  || | |  |  | |  |  | || | |||  |   |  |/-----+----+-+----++-++---+-----++++-++---++++-+---+\ ||  |         ||  ||  | | |
28
+|     ||   ||      |   \---+--++-+-+--+--+-+--+--/ || | |||  |   |  ||     |    | |    || ||   |     |||| ||   |||| |   || ||  |         ||  ||  | | |
29
+\-----++---++------+-------+--++-+-+--+--+-+--+----++-+-+++--+---+--++-----+----+-+----++-++---+-----/||| ||   |||| |   || ||  |         ||  ||  | | |
30
+      ||   ||      |       |  || |/+--+--+-+--+----++-+-+++--+---+--++\    |    | |/---++-++---+------+++-++\  |||| |   || ||  |         ||  ||  | | |
31
+      ||   ||      |       |  || |||  |  | |  |    || | |||  |   |  |||    |    | ||   || ||   |      ||| |||  |||| |   || ||  |         ||  ||  | | |
32
+      ||   ||      |       |  || |||  |  | |  |/---++-+-+++--+---+--+++----+\   | ||   || ||   |      ||| |||  |||| |   || ||  |         ||  ||  | | |
33
+      ||   ||      |       |  || |||  |  | |  ||   || | |||  |   |  |||    ||   | || /-++-++---+------+++-+++--++++-+---++-++--+--------\||  ||  | | |
34
+      ||   \+------+-------/  || |||  |  | |  ||   || | |||  |  /+--+++----++---+-++-+-++-++---+------+++\|||  |||| |   || |\--+--------+++--++--/ | |
35
+ /----++----+------+---\      \+-+++--+--+-+--++---++-+-+++--+--++--+++----++---+-++-+-++-/|   |      |||||||  |||| |   || |   |        |||  ||    | |
36
+ |    || /--+------+---+----\  | |||  |  | |/-++---++-+-+++--+--++--+++\   || /-+-++-+-++--+---+------+++++++--++++-+---++-+---+--------+++\ ||    | |
37
+ |    || |  |  /---+---+----+--+-+++--+--+-++-++--\|| | |||  |  ||  ||||/--++-+-+-++-+-++--+---+------+++++++--++++-+---++-+---+--------++++-++---\| |
38
+ |    || |  |  |   |   |    |  | |||  |  | |v ||  ||| | |||  |  ||  |||||  \+-+-+-++-+-++--+---+------+++++++--++++-+---++-+---+--------++++-+/   || |
39
+ |    || |  |  |   |   |    |  | |||  |  | || || /+++-+-+++--+--++--+++++---+-+-+-++-+-++--+---+------+++++++--++++-+---++-+---+---\    |||| |    || |
40
+ |    || |  |  |   \---+----+--+-+++--+--+-/| || |||| | |\+--+--++--+++++---+-+-+-++-+-++--+---+------++++/||  |||| |   || |   |   |    |||| |    || |
41
+ |    || |  |  |/------+-\  |  | |||  | /+--+-++-++++-+-+-+--+--++--+++++---+-+-+-++-+-++--+---+------++++-++-\|||| |   || |   |   |    |||| |    || |
42
+ | /--++-+--+--++------+\|  |  | |||  |/++--+-++-++++-+-+\|  |  ||  |||||   | | | |\-+-++--+---+------++++-+/ ||||| |   || |  /+---+----++++-+----++\|
43
+/+-+--++-+--+\ ||      |||  |  \-+++--++++--+-++-++++-+-+++--+--++--+++++---+-+-+-+--+-++--+---+------++++-+--+++++-+---/| |  ||   | /--++++-+--\ ||||
44
+|| |  ||/+--++-++------+++--+---\|||  ||||  | || |||| | |||  |  ||  |||||   | | \-+--+-++--+---/      |||| |  ||||| |    | |  ||   | |  |||| |  | ||||
45
+|| |  ||||  || ||      |||  |   ||||  ||||  | || |||| \-+++--+--++--+++++---+-+---+--+-++--+----------++++-/  ||||| |    | |  ||   | |  |||| |  | ||||
46
+|| |  v|||  || ||  /---+++--+---++++--++++--+-++-++++---+++--+--++--+++++---+-+---+--+-++--+-------\  ||||    ||||| |    | |  ||   | |  |||| |  | ||||
47
+|| |  ||||  || ||  |   |||  |   ||||  ||||  | || ||||   |||  |/-++--+++++---+-+---+--+-++--+\      |  ||||    |\+++-+----+-+--++---+-+--++++-/  | ||||
48
+|| |  ||||  || ||  |   |||  |  /++++--++++--+-++-++++---+++--++-++--+++++---+-+---+--+-++--++----\ |  ||||    | ||| |    | |  ||   | |  ||||    | ||||
49
+|| |  ||||  || ||  |   |||  |  ||||| /++++--+-++-++++---+++--++-++--+++++---+-+---+--+-++--++----+-+--++++----+-+++-+----+-+--++---+-+--++++----+\||||
50
+|| |  ||||  || ||  |   |||  |  ||||| |||||  | || |||\---+++--++-++--+++++---+-+---+--+-++--++----+-+--++++----+-/|| |    | |  ||   | |  ||||    ||||||
51
+|| |  ||||  || ||  |   |||  |  ||||| |||||  | ||/+++----+++--++-++--+++++---+-+---+--+-++--++----+-+--++++----+--++-+----+-+--++---+-+--++++\   ||||||
52
+|| |  ||||  || ||  |   |||  |  ||||| |||||  | ||||||    |||  || ||  |||||   | |   |  | ||  ||    | |  ||||    |  || |    | |  ||   | |  |||||   ||||||
53
+|| |  ||||  || ||  |   |||  |  |||||/+++++--+-++++++----+++--++-++--+++++---+-+---+--+-++--++----+-+--++++----+\ || |    | |  ||   | |  |||||   ||||||
54
+|| |  |\++--++-++--+---+++--+--+++++++++++--+-++++++----+++--++-++--+++++---+-+---+--+-++--++----+-+--++++----++-+/ |    | |  ||   | |  |||||   ||||||
55
+|| |  | ||  || ||  |   |||  |  |||||||||||  |/++++++----+++--++-++-\|||||   | |   |  | ||  ||    | |  ||||    || |  |    | |  ||   | |  |||||   ||||||
56
+|| |  | ||  || ||  |   |||  |  ||||\++++++--++++++++----/||  || || ||||||   | |   |  | ||  ||    | |  ||||    || |  |    | |  ||   | |  |||||   ||||||
57
+|| |  | ||  || ||  |   ||| /+--++++-++++++--++++++++-----++--++-++-++++++---+-+---+--+-++--++----+-+--++++----++-+--+----+-+--++---+-+\ |||||   ||||||
58
+|| |  | ||  || ||  |   ||| ||  |||| ||||||  |||\++++-----++--++-++-++++++---/ |   |  | ||  ||    | |  ||||    || |  |    | |  ||   | || |||||   ||||||
59
+|\-+--+-++--++-++--+---/|| ||  |||| ||||||  ||| ||||     ||  || || ||||||     |   |  | ||  ||    | |  ||||    || |  |    | |  ||   | || |||||   ||||||
60
+|  |  | ||  || ||  |    || ||  |||| ||||||  ||| |^||     || /++-++-++++++-----+---+\ | ||  ||   /+-+--++++----++-+--+----+-+--++---+-++-+++++--\||||||
61
+|  |  | ||  || ||  |    || ||  |||| ||||||  ||| ||||     || ||| || |\++++-----+---/| | ||  ||   || |  ||||    || |  |    | |  ||   | || |||||  |||||||
62
+| /+--+-++--++-++--+----++-++--++++-++++++--+++-++++-----++-+++-++-+-++++\    |    | | ||  ||   || |  ||||  /-++-+--+\   ^ |  ||   | || |||||  |||||||
63
+| ||  | ||  ||/++--+----++-++--++++-++++++--+++-++++-----++-+++-++-+-+++++----+----+-+-++--++---++-+\ ||||  | || |  ||   | |  ||   | || |||||  |||||||
64
+| ||  | ||  |||||  | /--++-++--++++-++++++--+++-++++-----++-+++-++-+-+++++----+----+-+-++--++---++-++-++++--+-++-+--++---+-+--++-\ | || |||||  |||||||
65
+| ||  | ||  ||||| /+-+--++-++--++++-++++++--+++-++++-----++-+++-++-+-+++++---\|    | | ||  ||   || ||/++++--+-++\|  ||   | |  || | | || |||||  |||||||
66
+| ||  | ||  ||||| || |  || ||  |||| ||||||  ||| |||| /---++-+++-++-+-+++++---++----+-+-++--++---++-+++++++--+-++++--++---+-+-\|| | | || |||||  |||||||
67
+| ||  | ||  ||||| || |  || ||  |||| \+++++--+++-++++-+---++-+++-++-+-+++++---++----+-+-++--++---++-+++++++--+-+/||  ||   | | ||| | | || |||||  |||||||
68
+| ||  | ||  |||||/++-+--++-++--++++--+++++--+++-++++-+---++-+++-++-+-+++++---++----+-+-++--++---++-+++++++--+-+-++\ ||   | | ||| | | || |||||  |||||||
69
+| ||  | ||  |||||||| |  || ||  |||\--+++++--+++-++++-+---++-+++-++-+-+/|||   ||    | | ||/-++---++-+++++++--+-+-+++-++---+-+-+++-+\| || |||||  |||||||
70
+| ||  | ||  |||||||| |  || ||  |||   |||||  ||| |||| |   || ||| || | | |||   ||    | | ||| ||   || |||||||  | | ||| ||   | | ||| ||| || |||||  |||||||
71
+| ||  | ||/-++++++++-+--++-++--+++---+++++--+++-++++-+---++-+++-++-+-+-+++---++----+-+-+++-++-\ || |||||||  | | |v| ||   | | ||| ||| || |||||  |||||||
72
+| ||  | ||| |||||||| |  || ||  |||   |||||  ||| |||| |   || ||| || | | |||   ||    | | ||| || | || |||||||  | | ||| ||   | | ||| ||| || |||||  |||||||
73
+| ||  | ||| ||||\+++-+--+/ || /+++---+++++--+++-++++-+---++-+++-++-+-+-+++---++----+-+-+++-++-+-++-+++++++--+-+-+++-++\  | | ||| ||| || |||||  |||||||
74
+| ||  | ||| |||| ||| |  |  || ||||   |||||  ||| |||| |   || ||| || | | |||   ||    | | ||| || | || |||||||  | | ||| |||  | | ||| ||| || |||||  |||||||
75
+| ||  | ||| |||| ||| |  |  || ||||   |||||  ||| |||| |/--++-+++-++-+-+\|||   ||    | | ||| || | || |||||||  | | ||| |||  | | ||| ||| || |||||  |||||||
76
+| ||  | ||| |||| ||| |  |  || ||||   |||||  ||| |||| ||  || \++-++-+-+++++---++----/ | ||| || | || |||||||  | | ||| |||  | | ||| ||| || |||||  |||||||
77
+| ||  |/+++-++++-+++\|  |  || ||||   |||||  ||| |||| ||  ||  || || | |||||   ||      | ||| || | || |||||||  | | ||| |||  | | ||| ||| || |||||  |||||||
78
+| ||  ||||| |||\-+++++--+--++-++++---+++++--+++-++/| ||  ||  || || | |||||   ||      | ||| || | || |||||||  | | ||| |||  | | ||| ||| || |||||  |||||||
79
+| ||  ||||| |||  |||||  |  || ||||   |||||  ||| || | ||  ||  || || | |||||   ||      | ||| || | || |||||||  | | ||| |||  | | ||| ||| || |||||  |||||||
80
+| ||  ||||| |||  |||||  |  || ||||   |||||  ||| || | ||  ||  || || | |||||   ||      | ||| \+-+-++-+++++++--+-+-+++-+++--+-+-+++-+++-++-+++++--++++++/
81
+| ||  ||||| |||  |||||  |  \+-++++---+++++--+++-++-+-++--++--++-++-+-+++++---++------+-+++--+-+-++-+++++++--+-+-+++-+++--+-+-+++-+++-+/ |||||  |||||| 
82
+| ||  ||||| |||  |||||  |   | |\++---+++++--+++-++-+-++--++--++-++-+-+++++---++------+-+++--+-+-+/ |||||||  | | ||| |||  | | ||| ||| |  |||||  |||||| 
83
+| ||  ||||| |||  |||||  |   | | |\---+++++--+++-++-+-++--++--/| || | |||||   ||      | |||  | | |  |||||||  | | ||| |||  | | ||| ||| |  |||||  |||||| 
84
+| ||  ||||| |||  |||||  |/--+-+-+----+++++--+++-++-+-++--++\  | || | \++++---++------+-+++--+-+-+--+++++++--+-+-+++-+++--/ | ||| ||| |  |||||  |||||| 
85
+| ||  ||||| |||  |||||  ||  | | |    |||||  ||| || | ||  |||  | || |  ||||   ||      | |||  | | |  |||||||  | | ||| |||    | ||| ||| |  |||||  |||||| 
86
+| ||  ||||| |||  |||||  ||  | | |    |||||  ||| || | ||  |||  | || |  ||||   ||      | |||  | | |  |||||||  \-+-+++-+/|    | ||| ||| |  |||||  |||||| 
87
+| ||  ||||| |||  |||||  ||  | | |    ||\++--+++-++-+-++--/||  | || |  ||||   ||      | |||  | | \--+++++++----+-+++-+-+----+-+++-+++-+--+++++--/||||| 
88
+| ||  ||||| |||  |||||  ||  | | |    || ||  ||| || | ||   ||  | || |  ||||   ||      | |||  | |    |||||||    | ||| | |    | ||| ||| |  |||||   ||||| 
89
+| ||  ||||| |||  |||||  ||  |/+-+----++-++--+++-++-+-++---++--+-++-+--++++---++------+-+++--+-+----+++++++--\ | ||| | |    | ||| ||| |  |||||   ||||| 
90
+| ||  ||||| |||  |||||  || /+++-+----++-++--+++-++-+-++---++--+-++-+--++++---++------+-+++--+-+\   |||||||  | | ||| | |    | ||| ||| |  |||||   ||||| 
91
+| ||  ||||| |||  |||||  || |||| |    || ||  ||| || | ||   ||  | || |  ||||   || /----+-+++--+-++---+++++++--+-+-+++-+-+----+-+++-+++-+--+++++---+++++\
92
+| ||  ||||| |||  |||||  || |||| |    || ||  ||| || | ||/--++--+-++-+--++++---++-+----+-+++--+-++---+++++++\ | | ||| | |    | ||| ||| |  |||||   ||||||
93
+| ||  ||||| |||  |||||  || |||| |/---++-++--+++-++-+\|||  ||  | |\-+--++++---++-+----+-+++--+-++---+++++/|| | | ||| | |    | ||| ||| |  |||||   ||||||
94
+| ||/-+++++-+++--+++++--++-++++-++\  || ||  ||| || |||||  ||  | |  |  ||||   || |    | |||  | ||   ||\++-++-+-+-/|| | |    | ||| ||| |  |||||   ||||||
95
+| ||| ||||| |||  |||||  || |||| |||  || ||  ||\-++-+++++--++--+-+--+--++++---++-+----+-+++--+-++---++-+/ || | | /++-+-+--\ | ||| ||| |  |||||   ||||||
96
+| ||| ||||| |||  |||||  || |||| |||  || ||  ||  || |||||  ||  | |  |  ||||   || |    \-+++--+-++---++-+--++-+-+-+++-+-+--+-+-+++-+++-+--/||||   ||||||
97
+| ||| ||||| |||  |||||  || |||| |||  || ||  || /++-+++++--++--+-+--+--++++---++-+------+++\ | ||   || |  || | | ||| | |  | | ||| ||| |   ||||   ||||||
98
+| |\+-+++++-+++--+++++--/| |||| |||  || ||  || ||| |||||  ||  | |  |  ||||   || |      |||| | ||   || \--++-+-+-+/| | |  | | ||| ||| \---++++---/|||||
99
+| | | ||||| |||  |||||   | |||| |||  || ||  || ||| |||||  ||  | |  |  ||||   || |      |||| | ||   ||    || | | | | | |  | | ||^ |v|     ||||    |||||
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
+  | |         || | |     | |  |   |      | |||  || |  \--+-+-+++-+-+--/|\++---+--+----------+--+----+------>----+++-+++--+----+++--+-------++-++--/ | 
132
+  | \---------++-+-+-----+-+--+---/      | |||  || |     | | |\+-+-+---+-++---+--+----------/  |    |           ||| |||  |    |||  |       || ||    | 
133
+  |           || | |     | \--+----------+-+++--++-+-----+-+-+-+-+-+---+-++---+--+-------------/    |           \++-+++--/    |||  |       || ||    | 
134
+  \-----------++-+-+-----+----+----------+-+++--++-+-----+-+-+-+-+-+---+-/|   |  |                  |            || |||       |||  |       || ||    | 
135
+              || | |     |    |          | \++--++-+-----+-+-+-+-/ \---+--+---+--+------------------+------------++-+++-------+++--+-------++-+/    | 
136
+              || | |     |    |          \--++--++-+-----+-+-+-+-------+--+---+--+------------------+------------/| |||       |||  |       || |     | 
137
+              || | |     |    |             \+--++-+-----+-+-+-+-------/  |   |  |                  |             | |||       |||  |       || |     | 
138
+              || | |     |    |              |  || \-----+-+-/ |          |   \--+------------------+-------------+-+++-------+++--+-------/| |     | 
139
+              || | \-----+----+--------------/  |\-------+-+---+----------+------+------------------+-------------+-+++-------+++--/        | |     | 
140
+              || |       \----+-----------------+--------+-/   |          |      |                  |             | |||       |||           | |     | 
141
+              || |            |                 |        |     |          |      |                  |             | |||       |||           | |     | 
142
+              || |            |                 |        |     |          |      |                  |             | |||       |||           | |     | 
143
+              || \------------+-----------------+--------+-----+----------+------+------------------+-------------/ |||       |||           | |     | 
144
+              ||              |                 |        \-----+----------+------+------------------+---------------+/|       \++-----------+-+-----/ 
145
+              ||              |                 |              |          |      |                  |               | |        ||           | |       
146
+              ||              |                 \--------------+----------+------+------------------+---------------+-+--------++-----------/ |       
147
+              |\---<----------+--------------------------------+----------+------/                  |               | |        ||             |       
148
+              |               |                                \----------/                         |               \-+--------/\-------------/       
149
+              |               \---------------------------------------------------------------------+-----------------/                               
150
+              \-------------------------------------------------------------------------------------/                                                 

+ 6 - 0
inputs/13_test.txt

@@ -0,0 +1,6 @@
1
+/->-\        
2
+|   |  /----\
3
+| /-+--+-\  |
4
+| | |  | v  |
5
+\-+-/  \-+--/
6
+  \------/   

+ 338 - 0
src/day13.rs

@@ -0,0 +1,338 @@
1
+extern crate regex;
2
+
3
+use std::collections::{HashMap, HashSet};
4
+use std::error::Error;
5
+use std::fs;
6
+use std::result;
7
+use std::str::FromStr;
8
+
9
+type Result<T> = result::Result<T, Box<Error>>;
10
+
11
+const INPUT: &str = "inputs/13.txt";
12
+
13
+#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
14
+pub struct Vector {
15
+    pub y: usize,
16
+    pub x: usize,
17
+}
18
+
19
+#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
20
+enum Turn {
21
+    Left,
22
+    Straight,
23
+    Right,
24
+    Reverse,
25
+}
26
+
27
+const INTER_SEQ: [Turn; 3] = [Turn::Left, Turn::Straight, Turn::Right];
28
+
29
+#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
30
+enum Direction {
31
+    North,
32
+    South,
33
+    East,
34
+    West,
35
+}
36
+
37
+#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
38
+struct Cart {
39
+    position: Vector,
40
+    direction: Direction,
41
+    next_turn: u8,
42
+}
43
+
44
+#[derive(Debug, PartialEq)]
45
+struct Track {
46
+    carts: Vec<Cart>,
47
+    turns: HashMap<Vector, (Direction, Direction)>,
48
+    intersections: HashSet<Vector>,
49
+}
50
+
51
+impl FromStr for Track {
52
+    type Err = Box<Error>;
53
+
54
+    fn from_str(s: &str) -> Result<Track> {
55
+        let mut carts = vec![];
56
+        let mut turns = HashMap::new();
57
+        let mut intersections = HashSet::new();
58
+        let mut incomplete_circuits: HashMap<(usize, usize), usize> = HashMap::new();
59
+
60
+        for (row_index, row) in s.lines().enumerate() {
61
+            let mut top_start: Option<usize> = None;
62
+            let mut bottom_start: Option<usize> = None;
63
+            for c in row.char_indices() {
64
+                match c {
65
+                    (col_index, '\\') => match top_start {
66
+                        Some(start) => {
67
+                            incomplete_circuits.insert((start, col_index), row_index);
68
+                            turns.insert(
69
+                                Vector {
70
+                                    x: col_index,
71
+                                    y: row_index,
72
+                                },
73
+                                (Direction::South, Direction::West),
74
+                            );
75
+                            top_start = None;
76
+                        }
77
+                        None => {
78
+                            bottom_start = Some(col_index);
79
+                            turns.insert(
80
+                                Vector {
81
+                                    x: col_index,
82
+                                    y: row_index,
83
+                                },
84
+                                (Direction::North, Direction::East),
85
+                            );
86
+                        }
87
+                    },
88
+                    (col_index, '/') => match bottom_start {
89
+                        Some(start) => match incomplete_circuits.remove(&(start, col_index)) {
90
+                            Some(top_row) => {
91
+                                turns.insert(
92
+                                    Vector {
93
+                                        x: col_index,
94
+                                        y: row_index,
95
+                                    },
96
+                                    (Direction::West, Direction::North),
97
+                                );
98
+                                bottom_start = None;
99
+                            }
100
+                            None => {
101
+                                return Err(From::from(
102
+                                    "Malformed track, circuit bottom without top",
103
+                                ))
104
+                            }
105
+                        },
106
+                        None => {
107
+                            top_start = Some(col_index);
108
+                            turns.insert(
109
+                                Vector {
110
+                                    x: col_index,
111
+                                    y: row_index,
112
+                                },
113
+                                (Direction::East, Direction::South),
114
+                            );
115
+                        }
116
+                    },
117
+                    (col_index, '+') => {
118
+                        intersections.insert(Vector {
119
+                            x: col_index,
120
+                            y: row_index,
121
+                        });
122
+                    }
123
+                    (col_index, '^') => carts.push(Cart {
124
+                        position: Vector {
125
+                            x: col_index,
126
+                            y: row_index,
127
+                        },
128
+                        direction: Direction::North,
129
+                        next_turn: 0,
130
+                    }),
131
+                    (col_index, 'v') => carts.push(Cart {
132
+                        position: Vector {
133
+                            x: col_index,
134
+                            y: row_index,
135
+                        },
136
+                        direction: Direction::South,
137
+                        next_turn: 0,
138
+                    }),
139
+                    (col_index, '>') => carts.push(Cart {
140
+                        position: Vector {
141
+                            x: col_index,
142
+                            y: row_index,
143
+                        },
144
+                        direction: Direction::East,
145
+                        next_turn: 0,
146
+                    }),
147
+                    (col_index, '<') => carts.push(Cart {
148
+                        position: Vector {
149
+                            x: col_index,
150
+                            y: row_index,
151
+                        },
152
+                        direction: Direction::West,
153
+                        next_turn: 0,
154
+                    }),
155
+                    _ => {}
156
+                }
157
+            }
158
+        }
159
+
160
+        Ok(Track {
161
+            carts,
162
+            turns,
163
+            intersections,
164
+        })
165
+    }
166
+}
167
+
168
+impl Direction {
169
+    fn turn(self, turn: Turn) -> Direction {
170
+        match turn {
171
+            Turn::Left => match self {
172
+                Direction::North => Direction::West,
173
+                Direction::South => Direction::East,
174
+                Direction::East => Direction::North,
175
+                Direction::West => Direction::South,
176
+            },
177
+            Turn::Right => match self {
178
+                Direction::North => Direction::East,
179
+                Direction::South => Direction::West,
180
+                Direction::East => Direction::South,
181
+                Direction::West => Direction::North,
182
+            },
183
+            Turn::Reverse => match self {
184
+                Direction::North => Direction::South,
185
+                Direction::South => Direction::North,
186
+                Direction::East => Direction::West,
187
+                Direction::West => Direction::East,
188
+            },
189
+            Turn::Straight => self,
190
+        }
191
+    }
192
+}
193
+
194
+impl Cart {
195
+    fn turn(&mut self, turn: Turn) {
196
+        self.direction = self.direction.turn(turn);
197
+    }
198
+
199
+    fn follow_turn(&mut self, turn_arms: (Direction, Direction)) {
200
+        let entering_from = self.direction.turn(Turn::Reverse);
201
+
202
+        if entering_from == turn_arms.0 {
203
+            self.direction = turn_arms.1;
204
+        } else if entering_from == turn_arms.1 {
205
+            self.direction = turn_arms.0;
206
+        } else {
207
+            panic!("Cart entered turn from an invalid direction");
208
+        }
209
+    }
210
+}
211
+
212
+impl Track {
213
+    fn run_tick(&mut self) -> Option<Vector> {
214
+        let mut cart_positions: HashSet<Vector> = HashSet::new();
215
+        for cart in self.carts.iter() {
216
+            cart_positions.insert(cart.position);
217
+        }
218
+
219
+        for cart in self.carts.iter_mut() {
220
+            let Vector { x, y } = cart.position;
221
+            cart_positions.remove(&cart.position);
222
+            cart.position = match cart.direction {
223
+                Direction::North => Vector { x, y: y - 1 },
224
+                Direction::South => Vector { x, y: y + 1 },
225
+                Direction::East => Vector { x: x + 1, y },
226
+                Direction::West => Vector { x: x - 1, y },
227
+            };
228
+
229
+            if let Some(turn_arms) = self.turns.get(&cart.position) {
230
+                cart.follow_turn(*turn_arms);
231
+            }
232
+
233
+            if self.intersections.contains(&cart.position) {
234
+                cart.turn(INTER_SEQ[cart.next_turn as usize]);
235
+                cart.next_turn = (cart.next_turn + 1) % INTER_SEQ.len() as u8;
236
+            }
237
+
238
+            if cart_positions.contains(&cart.position) {
239
+                return Some(cart.position);
240
+            }
241
+
242
+            cart_positions.insert(cart.position);
243
+        }
244
+        self.carts.sort_unstable();
245
+        None
246
+    }
247
+
248
+    fn find_first_collision(&mut self) -> Vector {
249
+        let mut collision: Option<Vector> = None;
250
+        while collision.is_none() {
251
+            collision = self.run_tick();
252
+        }
253
+        collision.unwrap()
254
+    }
255
+}
256
+
257
+fn read_track(filename: &str) -> Result<Track> {
258
+    let input = fs::read_to_string(filename)?;
259
+    Ok(input.parse()?)
260
+}
261
+
262
+pub fn solve_part1() -> Result<Vector> {
263
+    let mut track = read_track(INPUT)?;
264
+    Ok(track.find_first_collision())
265
+}
266
+
267
+#[cfg(test)]
268
+mod tests {
269
+    use super::*;
270
+
271
+    const TEST_INPUT: &str = "inputs/13_test.txt";
272
+    fn test_track() -> Track {
273
+        Track {
274
+            carts: vec![
275
+                Cart {
276
+                    position: Vector { x: 2, y: 0 },
277
+                    direction: Direction::East,
278
+                    next_turn: 0,
279
+                },
280
+                Cart {
281
+                    position: Vector { x: 9, y: 3 },
282
+                    direction: Direction::South,
283
+                    next_turn: 0,
284
+                },
285
+            ],
286
+            turns: vec![
287
+                (Vector { x: 0, y: 0 }, (Direction::East, Direction::South)),
288
+                (Vector { x: 4, y: 0 }, (Direction::South, Direction::West)),
289
+                (Vector { x: 4, y: 4 }, (Direction::West, Direction::North)),
290
+                (Vector { x: 0, y: 4 }, (Direction::North, Direction::East)),
291
+                (Vector { x: 7, y: 1 }, (Direction::East, Direction::South)),
292
+                (Vector { x: 12, y: 1 }, (Direction::South, Direction::West)),
293
+                (Vector { x: 12, y: 4 }, (Direction::West, Direction::North)),
294
+                (Vector { x: 7, y: 4 }, (Direction::North, Direction::East)),
295
+                (Vector { x: 2, y: 2 }, (Direction::East, Direction::South)),
296
+                (Vector { x: 9, y: 2 }, (Direction::South, Direction::West)),
297
+                (Vector { x: 9, y: 5 }, (Direction::West, Direction::North)),
298
+                (Vector { x: 2, y: 5 }, (Direction::North, Direction::East)),
299
+            ]
300
+            .iter()
301
+            .cloned()
302
+            .collect(),
303
+            intersections: vec![
304
+                Vector { x: 4, y: 2 },
305
+                Vector { x: 7, y: 2 },
306
+                Vector { x: 2, y: 4 },
307
+                Vector { x: 9, y: 4 },
308
+            ]
309
+            .iter()
310
+            .cloned()
311
+            .collect(),
312
+        }
313
+    }
314
+
315
+    #[test]
316
+    fn reads_track_file() {
317
+        let track = read_track(TEST_INPUT).unwrap();
318
+        assert_eq!(track, test_track());
319
+    }
320
+
321
+    #[test]
322
+    fn runs_one_tick() {
323
+        let mut track_after = test_track();
324
+        track_after.carts[0].position.x = 3;
325
+        track_after.carts[1].position.y = 4;
326
+        track_after.carts[1].direction = Direction::East;
327
+        track_after.carts[1].next_turn = 1;
328
+        let mut track = test_track();
329
+        track.run_tick();
330
+        assert_eq!(track, track_after);
331
+    }
332
+
333
+    #[test]
334
+    fn finds_first_collision() {
335
+        let mut track = test_track();
336
+        assert_eq!(track.find_first_collision(), Vector { x: 7, y: 3 });
337
+    }
338
+}

+ 7 - 3
src/main.rs

@@ -15,6 +15,7 @@ mod day9;
15 15
 mod day10;
16 16
 mod day11;
17 17
 mod day12;
18
+mod day13;
18 19
 
19 20
 fn main() {
20 21
     // println!("Day 1:");
@@ -53,7 +54,10 @@ fn main() {
53 54
     // println!("{},{}", part1.coord.x, part1.coord.y);
54 55
     // let part2 = day11::solve_part2().unwrap();
55 56
     // println!("{},{},{}", part2.coord.x, part2.coord.y, part2.size);
56
-    println!("Day 12:");
57
-    println!("{}", day12::solve_part1().unwrap());
58
-    println!("{}", day12::solve_part2().unwrap());
57
+    // println!("Day 12:");
58
+    // println!("{}", day12::solve_part1().unwrap());
59
+    // println!("{}", day12::solve_part2().unwrap());
60
+    println!("Day 13");
61
+    let part1 = day13::solve_part1().unwrap();
62
+    println!("{},{}", part1.x, part1.y);
59 63
 }