-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoc2021_14.R
40 lines (28 loc) · 982 Bytes
/
aoc2021_14.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
# Day 14
# Part 1
orig <- "NNCB"
orig_length_less_one <- str_length(orig) - 1
aoc2021_14_1 <- read_csv("data/aoc2021_14_1_example.txt", show_col_types = FALSE)
# Split into pairs
answer <- str("")
for (j in 1:10) {
for (i in 1:orig_length_less_one) {
two_char <- substring(orig, i, i+1)
char1 <- substring(two_char, 1, 1)
char2 <- substring(two_char, 2, 2)
zz <-match(two_char, aoc2021_14_1$KEY)
result <- str_trim(aoc2021_14_1$RESULT[zz])
if (i == orig_length_less_one) {
three_char <- paste(char1, result, char2)
} else {
three_char <- paste(char1, result)
}
answer <- str_replace_all(paste(answer, three_char), " ", "")
# cat("two_char: ", two_char, " char1: ", char1, " char2: ", char2, " zz: ", zz, " result:", result, " answer: ", answer, "\n")
}
cat("ANSWER: ", answer, "\n")
orig <- answer
orig_length_less_one <- str_length(orig) - 1
if (j < 10) { answer <- NULL }
}
count_chars(answer)