-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13-1.linq
220 lines (209 loc) · 29.3 KB
/
13-1.linq
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
39
40
41
42
43
44
45
46
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
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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<Query Kind="Program" />
TrackType[,] track;
List<Cart> carts;
int width = 0;
int height = 0;
void Main()
{
Setup();
//Print();
while (true)
{
foreach (var cart in carts.OrderBy(c => c.Y).ThenBy(c => c.X))
{
TrackType nextTrack = TrackType.None;
switch(cart.Bearing)
{
case Bearing.Up:
cart.Y -= 1;
nextTrack = track[cart.Y, cart.X];
break;
case Bearing.Down:
cart.Y += 1;
nextTrack = track[cart.Y, cart.X];
break;
case Bearing.Left:
cart.X -= 1;
nextTrack = track[cart.Y, cart.X];
break;
case Bearing.Right:
cart.X += 1;
nextTrack = track[cart.Y, cart.X];
break;
}
if (nextTrack == TrackType.Intersection) cart.IntersectionsPassed = cart.IntersectionsPassed + 1;
cart.Bearing = CalculateNewBearing(cart.Bearing, nextTrack, cart.IntersectionsPassed);
foreach (var anyCart in carts)
{
if (carts.Count(c => c.X == anyCart.X && c.Y == anyCart.Y) > 1)
{
Console.WriteLine("{0},{1}", anyCart.X, anyCart.Y);
return;
}
}
}
}
}
Bearing CalculateNewBearing(Bearing bearing, TrackType trackType, int intersectionsPassed)
{
Turn turn;
if (intersectionsPassed % 3 == 0) turn = Turn.Right;
else if (intersectionsPassed % 3 == 1) turn = Turn.Left;
else turn = Turn.Straight;
switch (bearing)
{
case Bearing.Up:
if (trackType == TrackType.Intersection)
{
if (turn == Turn.Left) return Bearing.Left;
if (turn == Turn.Right) return Bearing.Right;
if (turn == Turn.Straight) return Bearing.Up;
}
if (trackType == TrackType.LTRCorner) return Bearing.Left;
if (trackType == TrackType.RTLCorner) return Bearing.Right;
return Bearing.Up;
case Bearing.Down:
if (trackType == TrackType.Intersection)
{
if (turn == Turn.Left) return Bearing.Right;
if (turn == Turn.Right) return Bearing.Left;
if (turn == Turn.Straight) return Bearing.Down;
}
if (trackType == TrackType.LTRCorner) return Bearing.Right;
if (trackType == TrackType.RTLCorner) return Bearing.Left;
return Bearing.Down;
case Bearing.Left:
if (trackType == TrackType.Intersection)
{
if (turn == Turn.Left) return Bearing.Down;
if (turn == Turn.Right) return Bearing.Up;
if (turn == Turn.Straight) return Bearing.Left;
}
if (trackType == TrackType.LTRCorner) return Bearing.Up;
if (trackType == TrackType.RTLCorner) return Bearing.Down;
return Bearing.Left;
case Bearing.Right:
if (trackType == TrackType.Intersection)
{
if (turn == Turn.Left) return Bearing.Up;
if (turn == Turn.Right) return Bearing.Down;
if (turn == Turn.Straight) return Bearing.Right;
}
if (trackType == TrackType.LTRCorner) return Bearing.Down;
if (trackType == TrackType.RTLCorner) return Bearing.Up;
return Bearing.Right;
}
throw new Exception();
}
void Setup()
{
string inputString = @" /----------------------------------------------------------------------\ /------------\ ; /----------\ /-------------+------------------------------------------------\ | | | ; /-+----------+--+-------------+--\ | /-----------\ | | | ; | | | | | | /-----------------------+--+-----------+------+------------------+------------+-------\ ; | | | | | | | /------------+--+-----------+------+---------------\ | | | ; /-+-+----------+--+-------------+--+---------------------+-\ | /------+--+-----------+------+---------------+\ | | | ; /--+-+-+----------+--+-------------+--+---\ | | | | | | | | || | | | ; | | | | /--------+--+-------------+--+\ | | | | | | | | | || | /---------+-----\ | ; | | | | | /--+--+----->-------+--++--+----------------\| | | | | | | | /--------++-+--+---------+-\ | | ; | | | | | | | | | || | /<-------------++-+-\ /----+-----+------+--+-----------+------+------+--------++-+--+\ | | | | ; | | | | | | | | /+--++--+-+--------------++-+-+-+----+---->+------+--+-----------+------+------+--------++-+--++-----\ | | | | ; | | | | | | | | || || | | || |/+-+----+-----+------+--+-----------+------+\ | || | || | | | | | ; | | | | | | | /+------------++--++--+-+--------------++-+++-+-\ | | | | | || | || | || | | | | | ; | /+-+-+-+-----+--+-++------------++--++--+-+--------------++-+++-+-+--+-----+--\ | | | || | || | || | | | | | ; | || | | | | | || || || | | ||/+++-+-+--+-----+--+---+--+-----------+------++-----+--------++-+-\|| | | | | | ; \-++-+-+-+-----+--+-++------------++--++--/ | |||||| | |/-+-----+--+---+--+-----------+------++-----+-----\ || | ||| | | | | | ; || | | | | | || /--------++--++----+--------------++++++-+-++-+-----+--+---+--+-----------+------++--\ | | || | ||| | | | | | ; || | | | | | || | || || | /---------++++++-+-++-+-----+--+---+--+-----------+------++--+--+-\ | || | ||| | | | | | ; || | | | |/-+-++---+--------++--++----+----+\ /------++++++-+-++-+-----+--+---+--+-----\ | || | | | | || | ||| | | | | | ; || |/+-+-----++-+<++---+--------++--++----+\ || | |||||| | || | | | | | | | || | | | | || | ||| | | | | | ; || ||| | || | || | || || || || | |||||| \-++-+-----+--+---+--+-----+-----+------++--+--+-+---+--++-+-++/ | | | | | ; || ||| | || | || | || || || || | |||||| || | | | | | | | /++--+--+\| | || | || | | | | | ; || |||/+-----++-+-++---+--------++--++----++---++-+------++++++-\ || | | | | | | | /+++--+--+++---+->++-+\|| | | | | | ; || ||||| || | || | /------++--++----++---++-+------++++++-+-++-+-----+--+---+--+-----+-----+---\|||| | ||| | || |||| | | | | | ; || ||||\-----++-+-++---+-+------++--+/ || || | /---++++++-+-++-+-----+--+---+--+-----+----\| ||||| | ||| | || |||| | | | | | ; || |||| /-++-+-++---+-+------++--+-----++---++-+--+---++++++-+-++-+--\ | |/--+--+-----+----++---+++++--+--+++---+--++-++++------+--+-+---+-+-\; || |||| | || | || | | || | || || | | |\++++-+-++-+--+--+--++--+--+-----+----++---+++++--+--+++---+--++-++++------+--+-+---+-/ |; || |||| | || | ||/--+-+------++-\| /---++---++-+--+--\| |||| | || | | \--++--+--+-----+----++---+++++--+--+++---+--+/ |||| | | | | |; || |||| | || | |\+--+-+------++-++-+---++---++-+--+--++-++++-+-++-+--+-----++--/ | /---+----++---+++++--+--+++---+--+--++++------+--+-+\ | |; || |||| | || | | | | | || || | || || | | || |||| | || | | || /-+-+---+----++---+++++--+--+++---+--+\ |||| | | |^ | |; || |||| | || | | | | | || || | || || | | || ||||/+-++-+--+-----++---+-+-+---+----++---+++++--+\ ||| | || |||| | | || | |; || |||| | || | | | | | |\-++-+---++---++-+--+--++-++++++-++-+--+-----++---+-+-+---+----++---+++/| || ||| | || |||| | | || | |; || |||| | \+-+-+-+--+-+------+--++-+---++---++-+--+--+/ |||||| || | | /---++---+-+-+---+----++---+++-+--++-+++---+--++-++++------+\ | || | |; || |||| | | | | | | | | || | || ||/+--+--+--++++++-++-+--+-+---++---+-+-+---+----++---+++-+--++-+++---+--++-++++------++-+-++-\| |; |\-++++----+--+-+-+-+--+-+------+--++-+---++---++++--+--+--+/|||| || | | | || | | | | || ||| | || ||| | || |||| || | || || |; | |||\----+--+-+-+-+--+-+------+--++-+---++---++++--+--+--+-+++/ || | | | || | | | | || ||\-+--++-+/| | || |||| || | || || |; | ||| /+--+-+-+-+--+-+------+--++-+---++---++++--+--+--+-+++--++-+--+-+---++---+-+-+---+----++---++\ | || | | | || |||| || | || || |; | ||| || | | | | | | /---+--++-+---++---++++--+--+--+-+++--++-+--+-+---++---+-+-+---+----++---+++-+--++-+-+---+--++-++++---\ || | || || |; | ||| /--++--+-+-+-+--+-+--+---+--++-+---++---++++--+--+--+-+++--++-+--+-+---++\ | | | | || ||| | || | | | || |||| | || | || || |; | ||| | || | | | | | | /+---+--++-+---++---++++--+--+--+-+++--++-+--+-+-\ ||| | | | /+----++---+++-+--++-+-+---+--++-++++---+\ || | || || |; | ||| | || | | | | | | || | ||/+---++---++++--+--+--+-+++--++-+--+-+-+-+++--+-+-+--++----++---+++-+-\|| |/+---+--++-++++---++\|| | || || |; | ||| | || | | | | | | || | |||| || |||| | | | ||| |\-+--+-+-+-+++--+-+-+--++----++---+++-+-+++-+++---/ || |||| ||||| | || || |; | ||| | || | | | | | | || | |||\---++---++++--+--/ | ||| | | | | | ||| | |/+--++----++---+++-+-+++-+++------++-++++-\ ||||| | || || |; /+--+++-+--++--+-+-+-+--+-+-++---+--+++----++---++++--+-----+-+++--+--+--+-+-+\||| | ||| || /-++---+++-+-+++-+++------++-++++-+-+++++-+-++\|| |; || ||| | || | | | | | | || | ||| || |||| /+-----+-+++--+--+--+-+-+++++--+-+++-\|| /+-++---+++-+-+++-+++------++-++++-+-+++++-+-+++++--\|; || ||\-+--++--+-/ | | | | || | ||| /++---++++-++-----+-+++--+--+--+-+-+++++--+\||| ||| || || ||| | ||| ||| || |||| | ||||| | ||||| ||; || || | || /+---+-+--+-+-++---+--+++---+++---++++-++-----+-+++--+--+--+-+-+++++--+++++-+++-++-++---+++-+-+++-+++-----\|| |||| | ||||| | ||||| ||; || || | || || | | | | || | ||| ||| |||| || | ||| | | | | ||||| ||||| ||| || || ||| | ||| ||| ||| |||| | ||||| | ||||| ||; |\--++--+--++-++---+-+--+-+-++---+--+++---+++---++++-++-----+-+++--+--+--+-+-++/|| ||||| ||| || || ||| | ||| ||| ||| |||| | ||||| | ||||| ||; | ||/-+--++-++---+-+--+-+-++---+--+++---+++---++++-++-----+-+++--+--+-\| | || || ||||| ||| || || |||/+-+++-+++-----+++-++++-+-+++++\| ||||| ||; | ||| | /++-++---+-+--+-+-++---+--+++---+++---++++-++-----+-+++--+--+-++-+-++-++--+++++-+++-++-++--\||||| ||| ||| ||| |||| | ||||||| ||||| ||; | ||| | ||| |\---+-+--+-+-++---+--+++---+++---+/|| || | ||| | | ||/+-++-++--+++++-+++-++-++--++++++-+++-+++-----+++-++++-+\||||||| ||||| ||; | ||| | ||| | /-+-+--+-+-++---+--+++---+++---+-++-++-----+-+++--+--+-++++-++-++--+++++-+++\|| || |||||| ||| |\+-----+++-++++-++++/|||| ||||| ||; | ||| | ||| | | | | | |/++---+--+++---+++---+-++-++-----+-+++\ | | |||| || || ||||| |||||| || |||||| ||| | | ||| ||||/++++-++++-+++++-\||; | ||| | ||v | | | | | |||| | ||| ||| | || || | |||| | | |||| || || ||||| |||||| || |||||| ||| | | ||| ||||||||| |||| ||||| |||; | ||| | ||| | | | | /+-++++---+--+++---+++>--+-++-++-----+-++++\| | |||| || || ||||| |||||| || |||||| ||| | | ||| ||||||||| |||| ||||| |||; | ||| | ||| | | | | || |||| | ||| ||| | || || /--+-++++++--+-++++-++-++--+++++-++++++-++--++++++-+++-+\| ||| ||||||||| |||| ||||| |||; | ||| | ||| | | | | || |||| | ||| ||| | || || | | |||||| | |||| || || ||||| |||||| || |||||| ||| ||| ||| ||||||||| |||| ||||| |||; | ||| | ||| | | | | || |||| | ||| ||| | || || | | |||||| | |||| || || ||||| |||||| || |||||| ||| ||| ||| ||||||||| |||| ||||| |||; | ||| | ||| | | | | || |||| | ||| ||| | || || | | |||||| | |||| || || ||||| |||||| || |||||| ||| ||| ||| ||||||||| |||| ||||| |||; | ||| | ||| | |/+-+-++-++++---+--+++---+++---+-++-++--+--+-++++++--+-++++-++-++--+++++-++++++-++--++++++\||| ||| ||| ||||||||| |||| ||||| |||; | ||| | |v| | ||| | || |||| | ||| ||| | || || | | |||||| | |||| || || ||||| |||||| || |||||||||| ||| ||| ||||||||| |||| ||||| |||; /+---+++-+-+++-+--+++-+-++\|||| | ||| ||| | || || | | |||||| | |||| || || ||||| |||||| || |||||||||| ||| ||| ||||||||| |||| ||||| |||; || ||| | |||/+--+++-+-+++++++---+--+++---+++---+-++-++--+--+-++++++--+-++++-++-++--+++++-++++++-++\ |||||||||| ||| ||| ||||||||| |||| ||||| |||; || ||| | ||||| ||| | ||||||| | ||| ||| | || || | | |||||| | |||| || || ||||| |||||| ||| |||||||||| ||| ||| ||||||||| |||| ||||| |||; || ||| |/+++++--+++-+-+++++++---+--+++---+++---+-++-++--+--+-++++++--+-++++-++-++--+++++-++++++-+++-++++++++++-+++-\ ||| ||||||||| |||| ||||| |||; || ||| ||||||| ||| |/+++++++---+--+++--\||| | || || | | |||||| | |||| || || ||||| |||||| ||| |||||||||| ||| | ||| ||||||||| |||| |||v| |||; || ||| ||||||\--+++-+++++++++---+--+++--++++---+-++-++--+--+-++++++--+-++++-++-++--+++++-++++++-+++-++++++++++-+++-+---/|| ||||||||| |||| ||||| |||; || ||| |||||| ||| ||||||||| /+--+++--++++---+-++-++--+--+-++++++--+\|||| || || ||||| |||||| ||| |||||||||| \++-+----++-+++++++++-++++-/|||| |||; || ||| |||||| ||| ||||||||| || ||| |||| | || || \--+-++++++--++++++-++-++--+++++-++++++-+++-++++++++++--/| | || ||||||||| |||| |||| |||; || ||| |||||| ||\-+++++++++--++--+++--++++---+-++-++-----+-+++++/ |||||| || ^| ||||| |||||| ||| |||||||||| | | || ||||||||| |||| |||| |||; ||/--+++-++++++---++--+++++++++--++--+++--++++---+-++-++-----+-+++++---++++++-++-++-\||||| |||||| ||| |||||||||| | | || ||||||||| |||| |||| |||; ||| ||| |||||| || ||||||||| || ||| |||| |/++-++-----+-+++++---++++++-++-++-++++++-++++++-+++-++++++++++\ | | || ||||||||| |||| |||| |||; ||| \++-++++++---++--+++++++++--++--+/| ||||/--++++-++-----+-+++++---++++++-++-++-++++++-++++++\||| ||||||||||| | | || ||||||||| |||| |||| |||; ||| || |||||| /++--+++++++++--++--+-+--+++++--++++-++-----+-+++++--<++++++-++-++-++++++\|||||||||| ||||||||||| | | || ||||||||| |||| |||| |||; ||| || |||||| ||| /+++++++++--++--+-+--+++++--++++-++-----+-+++++-\ |||||| || || |||\+++++++++++/| ||||||||||| | | || ||||||||| |||| |||| |||; ||| || |||||| ||| |||||||||| || | | ||\++--++++-++-----+-+/||| | |||||| || || ||| ||||||||||| | ||||||||||| | | || ||||||||| |||| |||| |||; ||| \+-++++++--+++-++++++++++--++--+-+--++-/| \+++-++-----+-+-+++-+-++++++-++-++-+++-+++++++++++-+-+++++++++++--/ | || ||||||||| |||| |||| |||; ||| | |||||| ||| |||||||||| || | | || | ||| || | | ||| | |||||| || || ||| \++++++++++-+-+++++++++++----+----++-+++++/||| |||| |||| |||; ||| | |||||| ||| |||||||||| || | | || | ||| || | | ||| | |||||\-++-++-+++--++++++++++-+-+++++++++++----+----++-+++++-+++-+/|| |||| |||; ||| | |||||| ||| |||||||||| || | | || | ||| || | | ||| |/+++++--++-++-+++--++++++++++-+-+++++++++++----+--\ || ||||| ||| | || |||| |||; ||| | |||||| ||\-++++++++++--++--+-+--++--+---+++-++-----+-+-+++-+++++++--++-++-+++--++++++++++-+-++++++/|||| | | || ||||| ||| | || |||| |||; ||| | |||||| || |||||||||| || /+-+--++--+--\||| || | | \++-+++++++--++-++-+++--++++++++++-+-++++++-++/| /-+--+-++\||||| ||| | || |||| |||; ||| | |||||| || |||||||||| || || | |\--+--++++-++-----+-+--++-+++++++--++-++-++/ |||||||||| | |||||| || | | | | |||||||| ||| | || |||| |||; ||| | ||||\+--++--++++++++++--++-++-+--+---+--++++-++-----+-+--++-+++++/| || || || |||||||||| | |||||| || | | | | |||||||| ||| v || |||| |||; ||| | |||| | || |||||||||| || || | | | |||| || /-+-+--++-+++++-+--++-++-++---++++++++++-+-++++++-++-+--+-+-\| |||||||| ||| | || |||| |||; ||| | |||| |/-++--++++++++++--++-++-+--+---+\ |||| || | | | /++-+++++-+--++-++-++--\|||||||||| | |||||| || | | | || |||||||| ||| | || |||| |||; ||| /+-++++-++-++--++++++++++--++\|| | | || |\++-++---+-+-+-+++-+++++-+--++-++-++--+++++++++++-+-++++++-++-/ | | || |||\++++-+++-+-+/ |||v |||; ||| || |||| || || |||||||||| ||||| | | || | || || | | \-+++-+++++-+--++-++-++--+++++++++++-+-+++++/ || | | || ||| |||| ||| | | |||| |||; |\+---++-++++-++-++--++++++++++--+++++-+--+---++-+-++-++---+-+---+++-+++++-+--+/ || || ||||||||||| | ||||| || | | || ||| |||| ||| | | |||| |||; | | || |||| || || |||||||||| ||||| | | || | || \+---+-+---+++-+++++-+--+--++-++--+++/||||||| | ||||| || | | || ||| |||| ||| | | |||| |||; | | || |||| || || |||||||||| ||||| | | || | || | | | ||| ||||| | | \+-++--+++-+++++++-+-+++++--++----+-+-++-+++-++++-+++-+-+---++++-++/; | | || |||| || || |||||||||| ||||| | | || | \+--+---+-+---+++-+++++-+--+---+-++--+++-+++++++-+-+++++--++----+-+-++-+++-++++-+++-+-+---++/| || ; | | || |||| || || ||||\+++++--+++++-+--+---++-+--+--+---+-+---+++-+++++-+--+---+-++--+++-+++++++-+-+++++--+/ | | || ||| |||| ||| | | || | || ;/+-+---++-++++-++-++--++++-+++++--+++++\| | || | | \---+-+---+++-+++++-+--+---+-++--+++-++++++/ | ||||| | | | || ||| |||| ||| | | || | || ;|| | || |||| || || |||| ||||| |||\+++--+---++-/ | | | /+++-+++++-+--+---+\|| ||| |||||| |/+++++--+-----+-+-++-+++-++++-+++-+\| || | || ;|| | || |||| \+-++--++++-+++++--+++-+++--+---++----+------+-+--++++-+++++-+--+---++++--+++-++++++--/|||\++--+-----+-+-++-+++-/|\+-+++-+++---++-/ || ;|| | |\-++++--+-++--++++-+++++--+++-+++--+---++----+------+-+--++++-++++/ | | |||| ||| |||||| ||| || | | | || ||| | | ||| ||| || || ;|| | | |||| | || /++++-+++++--+++-+++--+---++----+-----\| | |||| |||| | | |||| ||| ||||\+---+++-++--+-----+-+-++-+++--+-+-+++-+++---+/ || ;|| | | |||| | || ||||| ||||| ||| ||| | || | || | |||| |||| | | /++++--+++-++++-+---+++-++--+-----+-+-++-+++--+-+-+++-+++---+----++\;|| | | |||| | || |||\+-+++++--+++-+++--/ || | |\-+--++++-++++--+--+--+++++--+++-++++-+---+++-++--+-----+-+-/| ||| | | ||| ||| | |||;|| | | |||| | || ||| | ||||| ||| ||| || | | | |||| |||| | | ||||| ||| |||| | ||| || | | | | ||| | | ||| ||| | |||;|| | | |||| /+-++-+++-+-+++++--+++-+++------++----+-----+--+--++++-++++--+--+--+++++--+++-++++-+---+++-++--+-----+-+\ | ||| | | ||| ||| | |||;|| | | |||| || \+-+++-+-+++++--+++-+++------++----+-----+--+--++++-++++--+--+--+++++--++/ |||| | ||| || | | || | ||| | | ||| ||| | |||;|| | | |||| || | ||| | ||||| ||| ||| /--++----+-----+--+--++++-++++--+--+--+++++--++--++++-+---+++-++--+-----+-++-+-+++--+\| ||| ||| | |||;|| | | |||| || | ||| | ||||| ||| ||| | || | | | |||| |||| | | ||||| || |||| | ||| || | | || | ||| ||| ||| ||| | |||;|| | | |||| || | ||| | ||||| |\+-+++---+--++----+-----+--+--++++-++++--+--+--+++++--++--++++-+---+++-++--+-----+-++-+-+++--+++-+++-/|| | |||;|| | | |||| || | ||| | ||||| | | ||| | || | | \--++++-++++--+--+--+++++--++--++++-+---+++-++--+-----+-++-+-+++--/|| ||| || | |||;|| | /-+--++++-++--+-+++-+-+++++-\| | ||| | || | | /-++++-++++--+--+--+++++--++--++++-+---+++-++--+----\| || | ||| || ||| || | |||;|| | | | ||\+-++--+-+++-+-+++++-++-+-+++---+--++----+-----+---+-++++-++++--+--+--+++++--++--++++-+---+/| || | || || | ||| || ||| || | |||;|| | | | || | || | ||| | ||||| || | ||\---+--++----+-----+---+-++++-++++--+--+--+++++--++--++++-+---+-+-++--/ || || | ||| || ||| || | |||;|| | | | || | || | ||| | ||||| || | || | || | | | |||| |||| | | ||||| || |||| | | | || || || | ||| || ||| || | |||;|| | | | || | || | ||| | ||||| || | || | || | | | |||| ||\+--+--+--+++++--++--++++-+---+-+-++-------++-++-+-/|| || ||| || | |||;|| | | | || | || | \++-+-+++++-++-+-++----+--++----+-----/ | |||| || | | | |||||/-++--++++-+---+-+-++-------++-++-+--++---++-+++--++--\| |||;|| | | | || | ||/-+--++-+-+++++-++-+-++----+--++----+\ | |||| || | | /+--++++++-++--++++-+---+-+-++-------++-++-+--++---++-+++--++--++--\ |||;|| | | | || | ||| | || | ||||| || | || | || || | |||| || | | || |||||| || |||| | | | || || || | || || ||| || || | |||;|| | | | || | ||| | || | ||||| || | || | || || | \+++-++-+--+-++--++/||| || |||| | | | |\-------++-++-+--++---++-+++--+/ || | |||;|| | | | || | ||| | || | ||||| || | || | /++----++--------+--+++-++-+\ | || || ||| || |||| | /-+-+-+--------++-++-+--++\ || ||| | || | |||;|| | | | || | ||| | || | |||||/++-+-++----+-+++----++-----\ | ||| || || | || || ||| || |||\-+-+-+-+-+--------++-++-+--+++--++<+++--+---++--+-+/|;|| | | | || | ||| | || | |||||||| | || | ||| || | | ||| || || | || || |\+-++--+++--+-+-+-+-+--------++-++-+--/|| || ||| | || | | |;|| | | | || | ||| | || \-++++++++-+-++----+-+++----++-----+--+--++/ || || | || || | | || ||| | | | | | /----++-++-+---++--++-+++--+---++--+-+\|;|| | | | || | ||| | || |\++++++-+-++----+-+++----++-----+--+--++--++-++-+-++--++-+-+-++--+++--+-+-+-/ | | || || | || || ||| | || | |||;|| | | | || | ||| | || | |||||| | || | ||| || | | \+--++-++-+-++--++-+-+-/| ||| | | | | | || || | || || ||| | || | |||;|| | | | /++-+-+++-+--++---+-++++++-+-++----+\||| || | | | || || | \+--++-+-+--+--+++--+-+-+---+---+----++-++-+---++--++-+++--+---++--/ |||;|| | | | ||| | ||| | || | ||\+++-+-++----+++++----++-----+--+---+--++-++-+--+--++-+-+--+--+++--+-+-+---+---+----++-++-+---++--++-+/| | || |||;|| | | | ||| | ||| | |\---+-++-+++-+-/| ||||| \+-----+--+---+--++-++-+--+--++-+-+--+--+/| | | | | | || || | || |\-+-+--+---++----/||;|| | | | ||| | ||| | | | || ||| | | ||||| | | | | || || |/-+--++-+-+--+--+-+--+-+-+---+--\| || || | || | | | | || ||;|| | \-+-+++-+-+++-+--+----+-++-+/| | | ||||| | | | | || || || | \+-+-+--+--+-+--+-+-+---+--++----++-++-+---++--+--+-+--+---++-----+/;|| | | ||| | ||| | | | || | | | | ||||| | | | | || || || | | | | | | | | | | | |v || || | || | | | | || | ;|| | | ||| \-+++-+--+----+-++-+-+-+--+----+++++-----+-----+--+---+--++-++-++-+---+-+-+--+--+-+--+-+-+---/ || |\-++-+---/| | | | | || | ;|\-+---+-+++---+++-+--+----/ \+-+-+-+--+----+++++-----+-----+--+---/ || || || | | | | | | | | | | || | || | | | | | | || | ;| | | ||| ||| | | | | | | | ||||| | | | |^ || || | | | | | \-+--+-+-+------++----+--++-+----+--+--+-/ | || | ;| | | \++---+++-+--+-------+-+-+-+--+----+/|\+-----+-----+--+------++-++-++-+---+-+-+--+----+--/ | | || | || | | | | | || | ;| | | || ||| | | | \-+-+--+----+-+-+-----+-----/ | |\-++-++-+---+-+-+--+----+----+-+------++----+--++-/ | | | | || | ;| | \--++---+++-+--+-------+---+-/ | | | | | | | || || | | | | \----+----+-+------++----+--++------+--+--+----+---+/ | ;| | || ||| | \-------+---+----+----+-+-+-----+--------+------/ || || | | | | | \-+------++----+--++------/ | | | | | ;| | || ||\-+----------+---+----+----+-+-+-----/ | || || | | | | | | |\----+--++---------+--+----+---+------/ ;| | || || | | \----+----+-+-+--------------+---------/| || | | | | | | | | || | | | | ;| \------++---++--+----------+--------+----+-+-+--------------+----------+-++-+---+-/ | | | | | || | | | | ;| |\---++--+----------+--------+----+-+-+--------------+----------+-++-+---+---+-------+------+------+-----+--/| | | | | ;| | || | | | | | | | | || | | | | | | | | | | | | ;| | || | | | | | | | | |\-+---+---+-------+------+------/ | | | | | | ;\---------+----++--+----------+--------/ \-+-+--------------+----------+-+--+---+---+-------+------+------------+---+---------/ | | | ; | |\--+----------+---------------+-/ | | \--+---+---+-------+------+------------+---+------------/ | | ; | | | | \----------------+----------/ | | | | | | | | | ; \----+---+----------+--------------------------------+---------------+---/ | | | | | | | ; | | | \---------------+-------+-------+------+------------/ | | | ; | | \------------------------------------------------/ \-------+------+----------------+-----------------+---/ ; \---+---------------------------------------------------------------------------+------+----------------/ | ; \---------------------------------------------------------------------------/ \----------------------------------/ ";
string[] inputLines = inputString.Split(';');
width = inputLines[0].Count();
height = inputString.Count(c => c == ';') + 1;
track = new TrackType[height, width];
carts = new List<Cart>();
int y = 0;
foreach (var inputLine in inputLines)
{
for (int x = 0; x < inputLine.Length; x++)
{
switch (inputLine[x])
{
case ' ':
track[y, x] = TrackType.None;
break;
case '|':
track[y, x] = TrackType.UpDown;
break;
case '-':
track[y, x] = TrackType.LeftRight;
break;
case '\\':
track[y, x] = TrackType.LTRCorner;
break;
case '/':
track[y, x] = TrackType.RTLCorner;
break;
case '+':
track[y, x] = TrackType.Intersection;
break;
case 'v':
track[y, x] = TrackType.UpDown;
carts.Add(new Cart { X = x, Y = y, Bearing = Bearing.Down });
break;
case '^':
track[y, x] = TrackType.UpDown;
carts.Add(new Cart { X = x, Y = y, Bearing = Bearing.Up });
break;
case '<':
track[y, x] = TrackType.LeftRight;
carts.Add(new Cart { X = x, Y = y, Bearing = Bearing.Left });
break;
case '>':
track[y, x] = TrackType.LeftRight;
carts.Add(new Cart { X = x, Y = y, Bearing = Bearing.Right });
break;
}
}
y++;
}
}
public class Cart
{
public int X { get; set; }
public int Y { get; set; }
public Bearing Bearing { get; set; }
public int IntersectionsPassed { get; set; }
}
public enum Bearing
{
Up,
Down,
Left,
Right
}
public enum Turn
{
Left,
Right,
Straight
}
public enum TrackType
{
None,
UpDown,
LeftRight,
LTRCorner,
RTLCorner,
Intersection
}
public void Print()
{
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
List<Cart> cartsHere = carts.Where(c => c.X == x && c.Y == y).ToList();
if (cartsHere.Count > 0)
{
if (cartsHere.Count > 1) Console.Write('X');
else if (cartsHere[0].Bearing == Bearing.Up) Console.Write('^');
else if (cartsHere[0].Bearing == Bearing.Down) Console.Write('v');
else if (cartsHere[0].Bearing == Bearing.Left) Console.Write('<');
else if (cartsHere[0].Bearing == Bearing.Right) Console.Write('>');
}
else
{
if (track[y, x] == TrackType.None) Console.Write(' ');
else if (track[y, x] == TrackType.LeftRight) Console.Write('-');
else if (track[y, x] == TrackType.UpDown) Console.Write('|');
else if (track[y, x] == TrackType.LTRCorner) Console.Write('\\');
else if (track[y, x] == TrackType.RTLCorner) Console.Write('/');
else if (track[y, x] == TrackType.Intersection) Console.Write('+');
}
}
Console.WriteLine();
}
Console.WriteLine();
}