-
Notifications
You must be signed in to change notification settings - Fork 1
/
day12.Rmd
113 lines (95 loc) · 3.69 KB
/
day12.Rmd
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
---
title: "--- Day 12: Rain Risk ---"
author: Fleur Kelpin
date: Dec 12, 2020
output: github_document
---
```{r message=FALSE, warning=FALSE}
library(tidyverse)
input <- read_csv(file = "day12.txt", col_names = "input") %>%
extract(input, "(\\w)(\\d+)", into = c("action", "value"), convert = TRUE)
input
```
# Part 1
The navigation instructions (your puzzle input) consists of a sequence of
single-character actions paired with integer input values. After staring at them
for a few minutes, you work out what they probably mean:
* Action N means to move north by the given value.
* Action S means to move south by the given value.
* Action E means to move east by the given value.
* Action W means to move west by the given value.
* Action L means to turn left the given number of degrees.
* Action R means to turn right the given number of degrees.
* Action F means to move forward by the given value in the direction the ship is
currently facing.
The ship starts by facing east. Only the L and R actions change the direction
the ship is facing. (That is, if the ship is facing east and the next
instruction is N10, the ship would move north 10 units, but would still move
east if the following action were F.)
Figure out where the navigation instructions lead. What is the Manhattan
distance between that location and the ship's starting position?
```{r}
rotation_matrix <- function(degrees) {
matrix(c(
cospi(degrees / 180), -sinpi(degrees / 180),
sinpi(degrees / 180), cospi(degrees / 180)
),
nrow = 2
)
}
```
```{r}
location <- c(0, 0)
direction <- c(1, 0)
for (i in 1:nrow(input)) {
value <- input$value[[i]]
switch(
input$action[[i]],
F = location <- location + direction * value,
N = location <- location + c(0, 1) * value,
E = location <- location + c(1, 0) * value,
S = location <- location + c(0, -1) * value,
W = location <- location + c(-1, 0) * value,
R = direction <- (rotation_matrix(value) %*% direction)[, 1],
L = direction <- (rotation_matrix(-value) %*% direction)[, 1]
)
}
sum(abs(location))
```
# Part 2
Before you can give the destination to the captain, you realize that the actual action meanings were printed on the back of the instructions the whole time.
Almost all of the actions indicate how to move a waypoint which is relative to
the ship's position:
* Action N means to move the waypoint north by the given value.
* Action S means to move the waypoint south by the given value.
* Action E means to move the waypoint east by the given value.
* Action W means to move the waypoint west by the given value.
* Action L means to rotate the waypoint around the ship left (counter-clockwise)
the given number of degrees.
* Action R means to rotate the waypoint around the ship right (clockwise) the
given number of degrees.
* Action F means to move forward to the waypoint a number of times equal to the
given value.
The waypoint starts 10 units east and 1 unit north relative to the ship. The
waypoint is relative to the ship; that is, if the ship moves, the waypoint moves
with it.
Figure out where the navigation instructions actually lead. What is the
Manhattan distance between that location and the ship's starting position?
```{r}
location <- c(0, 0)
waypoint <- c(10, 1)
for (i in 1:nrow(input)) {
value <- input$value[[i]]
switch(
input$action[[i]],
F = location <- location + waypoint * value,
N = waypoint <- waypoint + c(0, 1) * value,
E = waypoint <- waypoint + c(1, 0) * value,
S = waypoint <- waypoint + c(0, -1) * value,
W = waypoint <- waypoint + c(-1, 0) * value,
R = waypoint <- (rotation_matrix(value) %*% waypoint)[, 1],
L = waypoint <- (rotation_matrix(-value) %*% waypoint)[, 1]
)
}
sum(abs(location))
```