Petra Lamborn 11 months ago
parent
commit
3120f01176
2 changed files with 798 additions and 0 deletions
  1. 50
    0
      12.R
  2. 748
    0
      input12.txt

+ 50
- 0
12.R View File

@@ -0,0 +1,50 @@
1
+library(stringr)
2
+library(dplyr)
3
+
4
+input <- readLines("input12.txt") %>% str_match("^(\\w)(\\d+)$") %>% as.data.frame(stringsAsFactors = FALSE)
5
+names(input) <- c("Orig", "Arg", "Param")
6
+
7
+input %>% mutate(
8
+                 Param = as.numeric(Param),
9
+                 angChange = ifelse(Arg == "R", Param, ifelse(Arg == "L", -Param, 0)),
10
+                 Angle = cumsum(angChange) %% 360,
11
+                 MovEast = ifelse(Arg == "E", Param,
12
+                                  ifelse(Arg == "W", -Param,
13
+                                         ifelse(Arg == "F", cos(Angle * pi / 180) * Param, 0))),
14
+                 MovSouth = ifelse(Arg == "S", Param,
15
+                                  ifelse(Arg == "N", -Param,
16
+                                         ifelse(Arg == "F", sin(Angle * pi / 180) * Param, 0))),
17
+                 PosEast = cumsum(MovEast),
18
+                 PosSouth = cumsum(MovSouth)
19
+                 ) -> shipsLog
20
+
21
+mandist <- abs(sum(shipsLog$MovEast)) + abs(sum(shipsLog$MovSouth))
22
+
23
+shipsLog %>% select(Orig:Angle) %>%
24
+    mutate(
25
+           tPMovEast = ifelse(Arg == "E", Param, ifelse(Arg == "W", -Param, 0)),
26
+           tPMovSouth = ifelse(Arg == "S", Param, ifelse(Arg == "N", -Param, 0)),
27
+           PMovEast = ifelse(Angle == 0, tPMovEast,
28
+                             ifelse(Angle == 90, tPMovSouth,
29
+                                    ifelse(Angle == 180, -tPMovEast,
30
+                                           -tPMovSouth))),
31
+           PMovSouth = ifelse(Angle == 0, tPMovSouth,
32
+                              ifelse(Angle == 90, -tPMovEast,
33
+                                     ifelse(Angle == 180, -tPMovSouth,
34
+                                            tPMovEast))),
35
+           PREastPos = cumsum(PMovEast) + 10,
36
+           PRSouthPos = cumsum(PMovSouth) - 1,
37
+           PTEastPos = ifelse(Angle == 0, PREastPos,
38
+                              ifelse(Angle == 90, -PRSouthPos,
39
+                                     ifelse(Angle == 180, -PREastPos,
40
+                                            PRSouthPos))),
41
+           PTSouthPos = ifelse(Angle == 0, PRSouthPos,
42
+                              ifelse(Angle == 90, PREastPos,
43
+                                     ifelse(Angle == 180, -PRSouthPos,
44
+                                            -PREastPos))),
45
+           ShipMoveEast = ifelse(Arg == "F", Param * PTEastPos, 0),
46
+           ShipMoveSouth = ifelse(Arg == "F", Param * PTSouthPos, 0)
47
+           ) -> shipsLog2
48
+                                           
49
+
50
+mandist2 <- abs(sum(shipsLog2$ShipMoveEast)) + abs(sum(shipsLog2$ShipMoveSouth))

+ 748
- 0
input12.txt View File

@@ -0,0 +1,748 @@
1
+W5
2
+R90
3
+W3
4
+F98
5
+F87
6
+R90
7
+F57
8
+R180
9
+F3
10
+L90
11
+F60
12
+N3
13
+F94
14
+N3
15
+E2
16
+S2
17
+W2
18
+L90
19
+F26
20
+R90
21
+W1
22
+F58
23
+S5
24
+F22
25
+N3
26
+F95
27
+N1
28
+W1
29
+F11
30
+R180
31
+S3
32
+R270
33
+N1
34
+N1
35
+E4
36
+S3
37
+F81
38
+W2
39
+S2
40
+L90
41
+S4
42
+R90
43
+S1
44
+E1
45
+L90
46
+S4
47
+E2
48
+F72
49
+S1
50
+W1
51
+F73
52
+W4
53
+L180
54
+S1
55
+W5
56
+S5
57
+R90
58
+E3
59
+N3
60
+F28
61
+N1
62
+F84
63
+R90
64
+E5
65
+F13
66
+W5
67
+L90
68
+F11
69
+E4
70
+F63
71
+S1
72
+S2
73
+L90
74
+N3
75
+S5
76
+F20
77
+W1
78
+S4
79
+W1
80
+S2
81
+F67
82
+N1
83
+R180
84
+F11
85
+E1
86
+R90
87
+S2
88
+R90
89
+F38
90
+S5
91
+F27
92
+S5
93
+W3
94
+S3
95
+L90
96
+N2
97
+W2
98
+S1
99
+N4
100
+R90
101
+E1
102
+F16
103
+L90
104
+E4
105
+N1
106
+L90
107
+F89
108
+E5
109
+F90
110
+E1
111
+L180
112
+N1
113
+E3
114
+S2
115
+F58
116
+S3
117
+F53
118
+R90
119
+F100
120
+W1
121
+F53
122
+W5
123
+L90
124
+W3
125
+N4
126
+F54
127
+R180
128
+S2
129
+E2
130
+F63
131
+L90
132
+S4
133
+F40
134
+F90
135
+N3
136
+F83
137
+E5
138
+F35
139
+W4
140
+W5
141
+S3
142
+E2
143
+S2
144
+S1
145
+F12
146
+L90
147
+S1
148
+F41
149
+R90
150
+S3
151
+R90
152
+F66
153
+S2
154
+F34
155
+N5
156
+R90
157
+E5
158
+R90
159
+F25
160
+N5
161
+R90
162
+W1
163
+S2
164
+S2
165
+R90
166
+E3
167
+R90
168
+F95
169
+N3
170
+W2
171
+S4
172
+R90
173
+E3
174
+L90
175
+E3
176
+R90
177
+N2
178
+F84
179
+L90
180
+N2
181
+R90
182
+S1
183
+L90
184
+F93
185
+L90
186
+F60
187
+S4
188
+F85
189
+S2
190
+F84
191
+R180
192
+W5
193
+N4
194
+W5
195
+R270
196
+S2
197
+E2
198
+L180
199
+W4
200
+R180
201
+W5
202
+F56
203
+E1
204
+F45
205
+W4
206
+R90
207
+L180
208
+S1
209
+W4
210
+S5
211
+F87
212
+R180
213
+S2
214
+F76
215
+R90
216
+F76
217
+S1
218
+E4
219
+F6
220
+S1
221
+E2
222
+F47
223
+S3
224
+W2
225
+F16
226
+F75
227
+E3
228
+F75
229
+E4
230
+R90
231
+N5
232
+W3
233
+F1
234
+S1
235
+F8
236
+E2
237
+F64
238
+R90
239
+W4
240
+S5
241
+R90
242
+N5
243
+R90
244
+E2
245
+N1
246
+E1
247
+L180
248
+F31
249
+L180
250
+E5
251
+L90
252
+N3
253
+R90
254
+F77
255
+E3
256
+F65
257
+E4
258
+R90
259
+W1
260
+N3
261
+E3
262
+F4
263
+R90
264
+E3
265
+N4
266
+F28
267
+R180
268
+N2
269
+L90
270
+S2
271
+L90
272
+N1
273
+W1
274
+L180
275
+E4
276
+F51
277
+W4
278
+F9
279
+S4
280
+R90
281
+W5
282
+S4
283
+R90
284
+E3
285
+W2
286
+F44
287
+R90
288
+E1
289
+L180
290
+S4
291
+F93
292
+S2
293
+F58
294
+R90
295
+F80
296
+L90
297
+E2
298
+F20
299
+R90
300
+F19
301
+S4
302
+F22
303
+W1
304
+S2
305
+F62
306
+N2
307
+E5
308
+F21
309
+L90
310
+F16
311
+W2
312
+F58
313
+E2
314
+F54
315
+N1
316
+F83
317
+N3
318
+E2
319
+F62
320
+S3
321
+L90
322
+E3
323
+L270
324
+F29
325
+N5
326
+L90
327
+S2
328
+F19
329
+E5
330
+R180
331
+F87
332
+R180
333
+S2
334
+F22
335
+W3
336
+S5
337
+F35
338
+E3
339
+N5
340
+R180
341
+E2
342
+R270
343
+N3
344
+F5
345
+L90
346
+W3
347
+S1
348
+L90
349
+S2
350
+R270
351
+N5
352
+L180
353
+F79
354
+N3
355
+F82
356
+N3
357
+F73
358
+N4
359
+F57
360
+L90
361
+W3
362
+F26
363
+N4
364
+E5
365
+N4
366
+F48
367
+R90
368
+F62
369
+R90
370
+F36
371
+E1
372
+F76
373
+R90
374
+N3
375
+F83
376
+E5
377
+L90
378
+S1
379
+F1
380
+E1
381
+L90
382
+F67
383
+W3
384
+L90
385
+F42
386
+E4
387
+S2
388
+L180
389
+F89
390
+N1
391
+E4
392
+S4
393
+W1
394
+S2
395
+L90
396
+F91
397
+R90
398
+F78
399
+N5
400
+F29
401
+W2
402
+R90
403
+W1
404
+R90
405
+E2
406
+F40
407
+E2
408
+F76
409
+R90
410
+E4
411
+L90
412
+W3
413
+S1
414
+W3
415
+N4
416
+F81
417
+W4
418
+F22
419
+N1
420
+W1
421
+F47
422
+E3
423
+R90
424
+N5
425
+W4
426
+L90
427
+F44
428
+L90
429
+F58
430
+S1
431
+R90
432
+E3
433
+F91
434
+N1
435
+W4
436
+N5
437
+L90
438
+F60
439
+F8
440
+S4
441
+F17
442
+E5
443
+N2
444
+L90
445
+F37
446
+L180
447
+W4
448
+L180
449
+W4
450
+F93
451
+S5
452
+F71
453
+R90
454
+N5
455
+E3
456
+F20
457
+R90
458
+N5
459
+E5
460
+R90
461
+S2
462
+R180
463
+S5
464
+L90
465
+F26
466
+E4
467
+F49
468
+E5
469
+S4
470
+E4
471
+N1
472
+L180
473
+F33
474
+E1
475
+L90
476
+S4
477
+E5
478
+N5
479
+L180
480
+F60
481
+S1
482
+F53
483
+W1
484
+F34
485
+E2
486
+N3
487
+E2
488
+S5
489
+F61
490
+F32
491
+F18
492
+L90
493
+W4
494
+N4
495
+R90
496
+E4
497
+L90
498
+F26
499
+S3
500
+W4
501
+S5
502
+E5
503
+S2
504
+R90
505
+S5
506
+R90
507
+E2
508
+R90
509
+N2
510
+S3
511
+L90
512
+S3
513
+S2
514
+L90
515
+E5
516
+F31
517
+R180
518
+E2
519
+F42
520
+R90
521
+W4
522
+L90
523
+E5
524
+S3
525
+F79
526
+R90
527
+F76
528
+W1
529
+F9
530
+N1
531
+F91
532
+R90
533
+N3
534
+F32
535
+L180
536
+N4
537
+W2
538
+F18
539
+N5
540
+L90
541
+S4
542
+L180
543
+F22
544
+E4
545
+R180
546
+S2
547
+L90
548
+W5
549
+F31
550
+L180
551
+E1
552
+F15
553
+W1
554
+S5
555
+E4
556
+F56
557
+L90
558
+F7
559
+S3
560
+E4
561
+F13
562
+R90
563
+S4
564
+F78
565
+R180
566
+E3
567
+N1
568
+R90
569
+W4
570
+F29
571
+S4
572
+L90
573
+S4
574
+W4
575
+L180
576
+F6
577
+F84
578
+E4
579
+L90
580
+F29
581
+E2
582
+F75
583
+E3
584
+N4
585
+F33
586
+L270
587
+S4
588
+F62
589
+E2
590
+N4
591
+F50
592
+E1
593
+E2
594
+L270
595
+F100
596
+R180
597
+S2
598
+F89
599
+N2
600
+W3
601
+F5
602
+E5
603
+N2
604
+F75
605
+S3
606
+F2
607
+S4
608
+W3
609
+F33
610
+S5
611
+R90
612
+E5
613
+F20
614
+S1
615
+L90
616
+S4
617
+F10
618
+W5
619
+S2
620
+L90
621
+E3
622
+E1
623
+S3
624
+F41
625
+E5
626
+L90
627
+F28
628
+S2
629
+F40
630
+S2
631
+S4
632
+N1
633
+F15
634
+W5
635
+W2
636
+R270
637
+E5
638
+R90
639
+E4
640
+F50
641
+R90
642
+S4
643
+E1
644
+N4
645
+F73
646
+L90
647
+W2
648
+L270
649
+E4
650
+S1
651
+F30
652
+S3
653
+W4
654
+L180
655
+W4
656
+F6
657
+R90
658
+F43
659
+N5
660
+W3
661
+W3
662
+R90
663
+S2
664
+R90
665
+F50
666
+W2
667
+S4
668
+L90
669
+L180
670
+W1
671
+L270
672
+E3
673
+N2
674
+E2
675
+F48
676
+W1
677
+L90
678
+N4
679
+L90
680
+E3
681
+F86
682
+L180
683
+F10
684
+S1
685
+F36
686
+S4
687
+F33
688
+N5
689
+L90
690
+F14
691
+S2
692
+E4
693
+N4
694
+R90
695
+S2
696
+W2
697
+N2
698
+L180
699
+N4
700
+R90
701
+L90
702
+E5
703
+S1
704
+F82
705
+W1
706
+S1
707
+F19
708
+E3
709
+F19
710
+S2
711
+W1
712
+F18
713
+L90
714
+F43
715
+W1
716
+N2
717
+E3
718
+L90
719
+S1
720
+F75
721
+L90
722
+E4
723
+F80
724
+N5
725
+F15
726
+F53
727
+N4
728
+E5
729
+L90
730
+E1
731
+W5
732
+R270
733
+F17
734
+F63
735
+W2
736
+F20
737
+E1
738
+S3
739
+F19
740
+E1
741
+F99
742
+S1
743
+W1
744
+F23
745
+L180
746
+S3
747
+W4
748
+F76