Skip to content

Instantly share code, notes, and snippets.

@ozjimbob
Created December 3, 2022 08:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ozjimbob/178eb8d6b0c7ba96f01a08e7b9faae73 to your computer and use it in GitHub Desktop.
Save ozjimbob/178eb8d6b0c7ba96f01a08e7b9faae73 to your computer and use it in GitHub Desktop.
data <- readLines("data_03.txt")
letters_all <- c(letters,LETTERS)
process_line <- function(x){
this_line <- data[x]
sl = nchar(this_line)
str1 <- substr(this_line,1,sl/2)
str2 <- substr(this_line,(sl/2)+1,sl)
chars1 <- strsplit(str1,"")[[1]]
chars2 <- strsplit(str2,"")[[1]]
dbl <- intersect(chars1,chars2)
score <- which(letters_all == dbl)
score
}
out <- lapply(1:length(data),process_line)
total <- sum(unlist(out))
print(total)
process_threes <- function(x){
l1 <- strsplit(data[x],"")[[1]]
l2 <- strsplit(data[x+1],"")[[1]]
l3 <- strsplit(data[x+2],"")[[1]]
this_char <- intersect(intersect(l1,l2),l3)
score <- which(letters_all == this_char)
score
}
out <- lapply(seq(from=1,to=length(data),by=3),process_threes)
total <- sum(unlist(out))
print(total)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment