-
Notifications
You must be signed in to change notification settings - Fork 0
/
day6.R
36 lines (32 loc) · 919 Bytes
/
day6.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
source("helpers.R")
streams <- c(
"mjqjpqmgbljsphdztnvjfqwrcgsmlb",
"bvwbjplbgvbhsrlpgdmjqwftvncz",
"nppdvjthqldpwncqszvftbrmjlhg",
"nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg",
"zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw"
)
find_marker <- function(stream, marker_size) {
chars <- ustrsplit(stream, "")
buffer <- c()
for (i in seq_along(chars)) {
char <- chars[[i]]
index <- match(char, buffer, nomatch = 0L)
buffer <- c(buffer, char)
buffer <- buffer[(index + 1L):length(buffer)]
if (length(buffer) == marker_size) {
catn(paste0(buffer, collapse = ""))
return(i)
}
}
NA_integer_
}
# Puzzle 11
sapply(streams, find_marker, marker_size = 4L)
cat("\n")
cat_solution(11L, find_marker(readFile("input6.txt"), marker_size = 4L))
cat("\n")
# Puzzle 12
sapply(streams, find_marker, marker_size = 14L)
cat("\n")
cat_solution(12L, find_marker(readFile("input6.txt"), marker_size = 14L))