-
Notifications
You must be signed in to change notification settings - Fork 0
/
day10.R
157 lines (116 loc) · 3.99 KB
/
day10.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
library(stringr)
## FUNCTIONS -------------------------------------------------------------------
# Define open brackets
open <- c("{", "(", "[", "<")
# Define bracket points
points <- c(3, 57, 1197, 25137)
names(points) <- c(")", "]", "}", ">")
# Check which type a bracket is
bracket_type <- function(x) {
type <- vector("numeric", length(x))
type[x %in% c("(", ")")] <- 1
type[x %in% c("{", "}")] <- 2
type[x %in% c("<", ">")] <- 3
type[x %in% c("[", "]")] <- 4
return(type)
}
find_illegal_bracket <- function(code) {
code <- unlist(strsplit(code, ""))
# Which brackets are open, which are closed?
open_close <- ifelse(code %in% open, TRUE, FALSE)
while (length(code) > 1) {
# If there are no more open-close pairs, return the rest of the code
if (all(diff(open_close) == 0)) return(paste0(code, collapse = ""))
# Find the first "open-close"-pair (TRUE, FALSE; i.e. where diff is -1)
open_idx <- which.max(diff(open_close) == -1)
close_idx <- open_idx + 1
# Check whether brackets match. If type is the same (i.e. diff == 0),
# remove pair.
if (diff(bracket_type(c(code[open_idx], code[close_idx]))) == 0) {
code <- code[-c(open_idx, close_idx)]
open_close <- ifelse(code %in% open, TRUE, FALSE)
} else {
# If they don't match, stop and return the illegal character
return(code[close_idx])
}
diff(open_close)
}
}
reverse_string <- function(x) {
out <- sapply(x, function(y) {
y <- paste0(rev(unlist(strsplit(y, ""))), collapse = "")
})
out <- unname(out)
return(out)
}
calculate_score_pt2 <- function(x) {
part2_points <- 1:4
names(part2_points) <- c("(", "[", "{", "<")
x <- unlist(strsplit(x, ""))
score <- 0
for (i in 1:length(x)) {
score <- score * 5
score <- score + part2_points[x[i]]
}
return(score)
}
## EXAMPLE ---------------------------------------------------------------------
# Find non-matching brackets
example_input <- c(
"[({(<(())[]>[[{[]{<()<>>",
"[(()[<>])]({[<{<<[]>>(",
"{([(<{}[<>[]}>{[]{[(<()>",
"(((({<>}<{<{<>}{[]{[]{}",
"[[<[([]))<([[{}[[()]]]",
"[{[{({}]{}}([{[{{{}}([]",
"{<[[]]>}<{[{[{[]{()[[[]",
"[<(<(<(<{}))><([]([]()",
"<{([([[(<>()){}]>(<<{{",
"<{([{{}}[<[[[<>{}]]]>[]]"
)
## PART 1
illegal_bracktes <- vector("character", length(example_input))
for (i in seq_along(example_input)) {
illegal_bracktes[i] <- find_illegal_bracket(example_input[i])
}
# Whenever it finds a single (closed) bracket, it is a corrputed line.
corrupted <- illegal_bracktes[str_length(illegal_bracktes) == 1]
# Calculate score
sum(points[corrupted])
## PART 2
# Complete incomplete lines. Our function already returned the "rest" of the
# code. We just have to invert the rest to get the closing lines.
rest <- illegal_bracktes[str_length(illegal_bracktes) > 1]
completion <- reverse_string(rest)
# Calculate score
scores <- vector("numeric", length(completion))
for (i in seq_along(completion)) {
scores[i] <- calculate_score_pt2(completion[i])
}
# Find middle score
sort(scores)[ceiling(length(scores) / 2)]
## ACTUAL DATA -----------------------------------------------------------------
# Find non-matching bracktes
## READ IN DATA
day10 <- readLines("inputs/input10.txt")
## PART 1
illegal_bracktes <- vector("character", length(day10))
for (i in seq_along(day10)) {
illegal_bracktes[i] <- find_illegal_bracket(day10[i])
}
# Whenever it finds an open bracket, it is a corrputed line.
corrupted <- illegal_bracktes[!illegal_bracktes %in% open]
# Calculate score
sum(points[corrupted])
## PART 2
# Complete incomplete lines. Our function already returned the "rest" of the
# code. We just have to invert the rest to get the closing lines.
rest <- illegal_bracktes[str_length(illegal_bracktes) > 1]
completion <- reverse_string(rest)
# Calculate score
scores <- vector("numeric", length(completion))
for (i in seq_along(completion)) {
scores[i] <- calculate_score_pt2(completion[i])
}
# Find middle score
sort(scores)[ceiling(length(scores) / 2)]