Browse Source

Comments, because this code does not document itself

Petra Lamborn 3 years ago
parent
commit
760564e79c
1 changed files with 14 additions and 3 deletions
  1. 14
    3
      12.R

+ 14
- 3
12.R View File

1
 library(stringr)
1
 library(stringr)
2
 library(dplyr)
2
 library(dplyr)
3
 
3
 
4
+# Make a dataframe of input, with column 2 as the letter and column 3 as the number
4
 input <- readLines("input12.txt") %>% str_match("^(\\w)(\\d+)$") %>% as.data.frame(stringsAsFactors = FALSE)
5
 input <- readLines("input12.txt") %>% str_match("^(\\w)(\\d+)$") %>% as.data.frame(stringsAsFactors = FALSE)
5
 names(input) <- c("Orig", "Arg", "Param")
6
 names(input) <- c("Orig", "Arg", "Param")
6
 
7
 
7
 input %>% mutate(
8
 input %>% mutate(
8
-                 Param = as.numeric(Param),
9
-                 angChange = ifelse(Arg == "R", Param, ifelse(Arg == "L", -Param, 0)),
10
-                 Angle = cumsum(angChange) %% 360,
9
+                 Param = as.numeric(Param), # Make number numeric type
10
+                 angChange = ifelse(Arg == "R", Param, ifelse(Arg == "L", -Param, 0)), # Calculate what each R/L does to the angle
11
+                 Angle = cumsum(angChange) %% 360, # Calculate running angle, within 0-359
12
+                 # Calculate amount to move East, South at each line
11
                  MovEast = ifelse(Arg == "E", Param,
13
                  MovEast = ifelse(Arg == "E", Param,
12
                                   ifelse(Arg == "W", -Param,
14
                                   ifelse(Arg == "W", -Param,
13
                                          ifelse(Arg == "F", cos(Angle * pi / 180) * Param, 0))),
15
                                          ifelse(Arg == "F", cos(Angle * pi / 180) * Param, 0))),
14
                  MovSouth = ifelse(Arg == "S", Param,
16
                  MovSouth = ifelse(Arg == "S", Param,
15
                                   ifelse(Arg == "N", -Param,
17
                                   ifelse(Arg == "N", -Param,
16
                                          ifelse(Arg == "F", sin(Angle * pi / 180) * Param, 0))),
18
                                          ifelse(Arg == "F", sin(Angle * pi / 180) * Param, 0))),
19
+                 # Running tally of position
17
                  PosEast = cumsum(MovEast),
20
                  PosEast = cumsum(MovEast),
18
                  PosSouth = cumsum(MovSouth)
21
                  PosSouth = cumsum(MovSouth)
19
                  ) -> shipsLog
22
                  ) -> shipsLog
20
 
23
 
24
+# Calculate manhattin distance
21
 mandist <- abs(sum(shipsLog$MovEast)) + abs(sum(shipsLog$MovSouth))
25
 mandist <- abs(sum(shipsLog$MovEast)) + abs(sum(shipsLog$MovSouth))
22
 
26
 
27
+# Drop stuff from previous dataframe that wont be used this time
23
 shipsLog %>% select(Orig:Angle) %>%
28
 shipsLog %>% select(Orig:Angle) %>%
24
     mutate(
29
     mutate(
30
+           # Calculate the east, south amount being specified by param value
25
            tPMovEast = ifelse(Arg == "E", Param, ifelse(Arg == "W", -Param, 0)),
31
            tPMovEast = ifelse(Arg == "E", Param, ifelse(Arg == "W", -Param, 0)),
26
            tPMovSouth = ifelse(Arg == "S", Param, ifelse(Arg == "N", -Param, 0)),
32
            tPMovSouth = ifelse(Arg == "S", Param, ifelse(Arg == "N", -Param, 0)),
33
+           # Convert to a rotating reference frame according to the angle
27
            PMovEast = ifelse(Angle == 0, tPMovEast,
34
            PMovEast = ifelse(Angle == 0, tPMovEast,
28
                              ifelse(Angle == 90, tPMovSouth,
35
                              ifelse(Angle == 90, tPMovSouth,
29
                                     ifelse(Angle == 180, -tPMovEast,
36
                                     ifelse(Angle == 180, -tPMovEast,
32
                               ifelse(Angle == 90, -tPMovEast,
39
                               ifelse(Angle == 90, -tPMovEast,
33
                                      ifelse(Angle == 180, -tPMovSouth,
40
                                      ifelse(Angle == 180, -tPMovSouth,
34
                                             tPMovEast))),
41
                                             tPMovEast))),
42
+           # Calculate running position of waypoint in rotated frame
35
            PREastPos = cumsum(PMovEast) + 10,
43
            PREastPos = cumsum(PMovEast) + 10,
36
            PRSouthPos = cumsum(PMovSouth) - 1,
44
            PRSouthPos = cumsum(PMovSouth) - 1,
45
+           # Return to original fixed reference frame
37
            PTEastPos = ifelse(Angle == 0, PREastPos,
46
            PTEastPos = ifelse(Angle == 0, PREastPos,
38
                               ifelse(Angle == 90, -PRSouthPos,
47
                               ifelse(Angle == 90, -PRSouthPos,
39
                                      ifelse(Angle == 180, -PREastPos,
48
                                      ifelse(Angle == 180, -PREastPos,
42
                               ifelse(Angle == 90, PREastPos,
51
                               ifelse(Angle == 90, PREastPos,
43
                                      ifelse(Angle == 180, -PRSouthPos,
52
                                      ifelse(Angle == 180, -PRSouthPos,
44
                                             -PREastPos))),
53
                                             -PREastPos))),
54
+           # Calculate movement of ship at each line
45
            ShipMoveEast = ifelse(Arg == "F", Param * PTEastPos, 0),
55
            ShipMoveEast = ifelse(Arg == "F", Param * PTEastPos, 0),
46
            ShipMoveSouth = ifelse(Arg == "F", Param * PTSouthPos, 0)
56
            ShipMoveSouth = ifelse(Arg == "F", Param * PTSouthPos, 0)
47
            ) -> shipsLog2
57
            ) -> shipsLog2
48
                                            
58
                                            
49
 
59
 
60
+# Calculate manhattin distance
50
 mandist2 <- abs(sum(shipsLog2$ShipMoveEast)) + abs(sum(shipsLog2$ShipMoveSouth))
61
 mandist2 <- abs(sum(shipsLog2$ShipMoveEast)) + abs(sum(shipsLog2$ShipMoveSouth))