-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday18.R
117 lines (84 loc) · 2.83 KB
/
day18.R
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
# Example input, if needed
# day18 <- readLines("./puzzle_input/input_day18_exp.txt")
day18 <- readLines("./puzzle_input/input_day18.txt")
day18 <- as.data.frame(do.call(rbind, strsplit(day18, " ")))
names(day18) <- c("direction", "steps", "colour")
day18$steps <- as.numeric(day18$steps)
day18$colour <- gsub("[()]", "", day18$colour)
## PART 1 ----------------------------------------------------------------------
start <- Sys.time()
# Get all node coordinates
day18$steps_converted <-
ifelse(day18$direction %in% c("L", "U"), -day18$steps, day18$steps)
day18$x <-
cumsum(ifelse(day18$direction %in% c("U", "D"), day18$steps_converted, 0)) + 1
day18$y <-
cumsum(ifelse(day18$direction %in% c("L", "R"), day18$steps_converted, 0)) + 1
day18 <-
rbind.data.frame(
data.frame(
direction = "",
steps = 0,
colour = "",
steps_converted = 0,
x = 1,
y = 1
),
day18
)
day18 <- day18[nrow(day18):1, ]
# The honest part is where I googled and found the Shoelace algorithm, but then
# I cheated a little bit because Reddit told me that I needed to take care of
# the outline as well.
a <- 0
for (i in 1:(nrow(day18) - 1)) {
a <- a + day18$x[i] * day18$y[i + 1] - day18$y[i] * day18$x[i + 1] +
day18$steps[i] # to include the outside border as well
}
# I don't have a clue why it's + 1 :-D
a / 2 + 1
# 68115
Sys.time() - start
## -----------------------------------------------------------------------------
# day18 <- readLines("./puzzle_input/input_day18_exp.txt")
day18 <- readLines("./puzzle_input/input_day18.txt")
day18 <- as.data.frame(do.call(rbind, strsplit(day18, " ")))
names(day18) <- c("direction", "steps", "colour")
day18$steps <- as.numeric(day18$steps)
day18$colour <- gsub("[()]", "", day18$colour)
## PART 2 ----------------------------------------------------------------------
start <- Sys.time()
day18$steps <- strtoi(substr(day18$colour, 2, 6), base = 16)
day18$direction <- substr(day18$colour, 7, 7)
day18$direction <-
factor(day18$direction, levels = 0:3, labels = c("R", "D", "L", "U"))
# Get coordinates
day18$steps_converted <-
ifelse(day18$direction %in% c("L", "U"), -day18$steps, day18$steps)
day18$x <-
cumsum(ifelse(day18$direction %in% c("U", "D"), day18$steps_converted, 0)) + 1
day18$y <-
cumsum(ifelse(day18$direction %in% c("L", "R"), day18$steps_converted, 0)) + 1
day18 <-
rbind.data.frame(
data.frame(
direction = "",
steps = 0,
colour = "",
steps_converted = 0,
x = 1,
y = 1
),
day18
)
day18 <- day18[nrow(day18):1, ]
a <- 0
for (i in 1:(nrow(day18) - 1)) {
a <- a + day18$x[i] * day18$y[i + 1] - day18$y[i] * day18$x[i + 1] +
day18$steps[i] # to include the outside border as well
}
options(scipen = 999)
# I don't have a clue why it's + 1 :-D
a / 2 + 1
# 71262565063800
Sys.time() - start