-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10.R
64 lines (57 loc) · 1.29 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
input <- strsplit(readLines("data/2021/day10.txt"), "")
# part 1
# find the 1st illegal character in each corrupted line of the navigation
# subsystem. what is the total syntax error score for those errors?
pts <- list(
"(" = -3, ")" = 3,
"[" = -57, "]" = 57,
"{" = -1197, "}" = 1197,
"<" = -25137, ">" = 25137
)
chk_line <- function(x) {
line_pts <- pts[x]
n <- length(line_pts)
i <- 2
while(i <= n) {
if(line_pts[[i]] > 0) {
if(line_pts[[i]] != -line_pts[[i - 1]]) {
return(line_pts[[i]])
}
line_pts[(i - 1):i] <- NULL
i <- i - 1
n <- n - 2
} else {
i <- i + 1
}
}
0
}
scores <- sapply(input, chk_line)
sum(scores)
# part 2
# find the completion string for each incomplete line, score the completion
# strings, and sort the scores. what is the middle score?
input <- input[scores == 0]
pts <- list(
"(" = 1, ")" = -1,
"[" = 2, "]" = -2,
"{" = 3, "}" = -3,
"<" = 4, ">" = -4
)
fill_line <- function(x) {
line_pts <- pts[x]
n <- length(line_pts)
i <- 2
while(i <= n) {
if(line_pts[[i]] < 0) {
line_pts[(i - 1):i] <- NULL
i <- i - 1
n <- n - 2
} else {
i <- i + 1
}
}
sum(unlist(line_pts) * 5^(seq_along(line_pts) - 1))
}
scores <- sapply(input, fill_line)
median(scores)