-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday13-2.p
320 lines (272 loc) · 10.7 KB
/
day13-2.p
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
--- 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.
*/
DEFINE VARIABLE c AS CHARACTER NO-UNDO.
DEFINE VARIABLE cTrack AS CHARACTER EXTENT 150 NO-UNDO.
DEFINE VARIABLE i AS INTEGER NO-UNDO.
DEFINE VARIABLE iCart AS INTEGER NO-UNDO.
DEFINE VARIABLE iLine AS INTEGER NO-UNDO.
DEFINE VARIABLE iTick AS INTEGER NO-UNDO.
DEFINE TEMP-TABLE ttCart NO-UNDO
FIELD id AS INTEGER
FIELD ix AS INTEGER
FIELD iy AS INTEGER
FIELD idx AS INTEGER
FIELD idy AS INTEGER
FIELD cLastTurn AS CHARACTER INITIAL "R"
INDEX ix IS PRIMARY UNIQUE id
INDEX ixy ix iy.
DEFINE BUFFER bttCart FOR ttCart.
ETIME(YES).
INPUT FROM C:\User\JCCARDOT\Perso\Travail\aoc\aoc2018\day13.txt.
REPEAT:
iLine = iLine + 1.
IMPORT UNFORMATTED cTrack[iLine].
/* detect carts */
DO i = LENGTH(cTrack[iLine]) TO 1 BY -1:
c = SUBSTRING(cTrack[iLine], i, 1).
IF LOOKUP(c, "<,>,^,v") > 0 THEN
RUN newCart(i, iLine, c).
END.
END.
INPUT CLOSE.
RUN drawTracks.
/* now let's run the carts! */
blkRun:
DO WHILE TRUE:
iTick = iTick + 1.
blkCart:
REPEAT PRESELECT EACH ttCart BY ttCart.ix BY ttCart.iy:
FIND NEXT ttCart.
/* what's in front of us? */
ASSIGN
ttCart.ix = ttCart.ix + ttCart.idx
ttCart.iy = ttCart.iy + ttCart.idy.
CASE SUBSTRING(cTrack[ttCart.iy], ttCart.ix, 1):
/* WHEN "-" OR WHEN "|" THEN . */
WHEN "\" THEN DO:
IF /* > */ idx = +1 AND idy = 0 THEN ASSIGN ttCart.idx = 0 ttCart.idy = +1.
ELSE /* < */ IF idx = -1 AND idy = 0 THEN ASSIGN ttCart.idx = 0 ttCart.idy = -1.
ELSE /* V */ IF idx = 0 AND idy = +1 THEN ASSIGN ttCart.idx = +1 ttCart.idy = 0.
ELSE /* ^ */ IF idx = 0 AND idy = -1 THEN ASSIGN ttCart.idx = -1 ttCart.idy = 0.
NEXT blkCart.
END.
WHEN "/" THEN DO:
IF /* > */ idx = +1 AND idy = 0 THEN ASSIGN ttCart.idx = 0 ttCart.idy = -1.
ELSE /* < */ IF idx = -1 AND idy = 0 THEN ASSIGN ttCart.idx = 0 ttCart.idy = +1.
ELSE /* V */ IF idx = 0 AND idy = +1 THEN ASSIGN ttCart.idx = -1 ttCart.idy = 0.
ELSE /* ^ */ IF idx = 0 AND idy = -1 THEN ASSIGN ttCart.idx = +1 ttCart.idy = 0.
NEXT blkCart.
END.
WHEN "+" THEN DO:
CASE ttCart.cLastTurn:
WHEN "R" THEN DO: /* turn left */
ttCart.cLastTurn = "L".
IF /* > */ idx = +1 AND idy = 0 THEN ASSIGN ttCart.idx = 0 ttCart.idy = -1.
ELSE /* < */ IF idx = -1 AND idy = 0 THEN ASSIGN ttCart.idx = 0 ttCart.idy = +1.
ELSE /* V */ IF idx = 0 AND idy = +1 THEN ASSIGN ttCart.idx = +1 ttCart.idy = 0.
ELSE /* ^ */ IF idx = 0 AND idy = -1 THEN ASSIGN ttCart.idx = -1 ttCart.idy = 0.
END.
WHEN "L" THEN DO: /* go straight */
ttCart.cLastTurn = "S".
END.
WHEN "S" THEN DO: /* turn right */
ttCart.cLastTurn = "R".
IF /* > */ idx = +1 AND idy = 0 THEN ASSIGN ttCart.idx = 0 ttCart.idy = +1.
ELSE /* < */ IF idx = -1 AND idy = 0 THEN ASSIGN ttCart.idx = 0 ttCart.idy = -1.
ELSE /* V */ IF idx = 0 AND idy = +1 THEN ASSIGN ttCart.idx = -1 ttCart.idy = 0.
ELSE /* ^ */ IF idx = 0 AND idy = -1 THEN ASSIGN ttCart.idx = +1 ttCart.idy = 0.
END.
END CASE.
END.
END CASE.
/* detect collision */
IF CAN-FIND(FIRST bttCart WHERE bttCart.id <> ttCart.id AND bttCart.ix = ttCart.ix AND bttCart.iy = ttCart.iy) THEN DO:
FOR EACH bttCart WHERE bttCart.id <> ttCart.id AND bttCart.ix = ttCart.ix AND bttCart.iy = ttCart.iy:
DELETE bttCart.
END.
DELETE ttCart.
END.
END. /* blkCart: FOR EACH ttCart */
RUN drawTracks.
IF CAN-FIND(ttCart) THEN /* there is only one left */
LEAVE blkRun.
END. /* blkRun: DO WHILE TRUE */
FIND ttCart NO-ERROR.
MESSAGE ETIME SKIP
iTick "ticks" SKIP
iCart "carts" SKIP
IF AVAILABLE ttCart THEN STRING(ttCart.ix - 1) + "," + STRING(ttCart.iy - 1) ELSE ""
VIEW-AS ALERT-BOX INFO BUTTONS OK.
/* 3531 */
/* 22879 ticks */
/* 17 carts */
/* 116,125 */
PROCEDURE newCart:
DEFINE INPUT PARAMETER pix AS INTEGER NO-UNDO.
DEFINE INPUT PARAMETER piy AS INTEGER NO-UNDO.
DEFINE INPUT PARAMETER pcC AS CHARACTER NO-UNDO.
iCart = iCart + 1.
CREATE ttCart.
ASSIGN ttCart.id = iCart
ttCart.ix = pix
ttCart.iy = piy.
/* clean up tracks - remove carts characters */
CASE pcC:
WHEN ">" THEN ASSIGN ttCart.idx = +1 ttCart.idy = 0 SUBSTRING(cTrack[iLine],i,1) = "-".
WHEN "<" THEN ASSIGN ttCart.idx = -1 ttCart.idy = 0 SUBSTRING(cTrack[iLine],i,1) = "-".
WHEN "v" THEN ASSIGN ttCart.idx = 0 ttCart.idy = +1 SUBSTRING(cTrack[iLine],i,1) = "|".
WHEN "^" THEN ASSIGN ttCart.idx = 0 ttCart.idy = -1 SUBSTRING(cTrack[iLine],i,1) = "|".
END CASE.
END PROCEDURE.
PROCEDURE drawTracks:
RETURN. /* do not draw ;) */
DEFINE BUFFER ttCart FOR ttCart.
OUTPUT TO c:/temp/track.txt APPEND.
PUT UNFORMATTED "Tick " iTick SKIP.
DO i = 1 TO 150:
c = cTrack[i].
IF c = "" THEN LEAVE.
FOR EACH ttCart WHERE ttCart.iy = i:
SUBSTRING(c, ttCart.ix, 1) =
IF CAN-FIND(FIRST bttCart WHERE bttCart.id <> ttCart.id AND bttCart.ix = ttCart.ix AND bttCart.iy = ttCart.iy) THEN "X"
ELSE IF idx = +1 AND idy = 0 THEN ">"
ELSE IF idx = -1 AND idy = 0 THEN "<"
ELSE IF idx = 0 AND idy = -1 THEN "^"
ELSE IF idx = 0 AND idy = +1 THEN "V"
ELSE "@".
END.
PUT UNFORMATTED c SKIP.
END.
PUT UNFORMATTED SKIP(1).
OUTPUT CLOSE.
END PROCEDURE.