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