-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday-3.R
36 lines (31 loc) · 796 Bytes
/
day-3.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
#data <- read.csv("day-3-ex.txt",header = F)
data <- read.csv("input-3.txt",header = F)
eval <- function(backpack){
lets <- c(letters,LETTERS)
complength <- nchar(backpack)/2
first <- substr(backpack,1,complength)
second <- substr(backpack, complength + 1, nchar(backpack))
for (i in 1:complength){
chr <- substr(first,i,i)
if (grepl(chr,second)){ return(match(chr, lets))}
}
}
eval(data$V1[1])
su <- 0
for (i in data$V1){
su <- su + eval(i)
}
print(su)
#part 2
eval2 <- function(bp1,bp2,bp3){
lets <- c(letters,LETTERS)
for (i in 1:nchar(bp1)){
chr <- substr(bp1,i,i)
if (grepl(chr,bp2)&grepl(chr,bp3)){return(match(chr,lets))}
}
}
su2 <- 0
for (i in 1:(length(data$V1)/3)){
su2 <- su2 + eval2(data$V1[3*i-2],data$V1[3*i-1],data$V1[3*i])
}
print(su2)