Find items (letters) occuring in the first and second half of a string, rank by position in the alphabet (1-26 for lowercase letters, 27-52 for uppercase letters) and calculate the sum.
split strings into first (c1) and second half (c2)
split each half into single letters
intersect first and half pairwise
match with alphabet and calculate sum
len =nchar(txt) c1 =substr(txt, start = 1L, stop = len / 2L)c2 =substr(txt, start = len / 2L + 1L, stop = len)c1 =strsplit(c1, split ="")c2 =strsplit(c2, split ="")items =Map( intersect , x = c1 , y = c2) |>unlist() score =match( items , table =c(letters, LETTERS))sum(score)
[1] 7875
Approach:
split strings into first (c1) and second half (c2)
intersect first and half pairwise
get unicode code point for uppercase and lowercase letters, use a different offset to get correct scores
calculate sum
c1 = [i[:int(len(i)/2)] for i in r.txt]c2 = [i[int(len(i)/2):] for i in r.txt]items = []for i1, i2 inzip(c1, c2): i =set(i1) &set(i2) items.append(list(i)[0])l = [ord(i) -96for i in items if i.islower()]u = [ord(i) -65+27for i in items if i.isupper()]sum(l + u)
per group, unify items per rucksack and then calculate group frequency
identify item with frequency of 3
match with alphabet and calculate sum
from =seq.int(1L, to =length(txt), by = 3L)to =seq.int(3L, to =length(txt), by = 3L)grp =mapply( \(f, t) txt[f:t] , f = from , t = to , SIMPLIFY =FALSE)stick =lapply( grp , FUN = \(g){ s =strsplit(g, split ="") |>lapply(FUN = unique) |>unlist() |>table()names(s)[which(s ==3)] }) |>unlist()score =match( stick , table =c(letters, LETTERS))sum(score)