2022 - day 03

Author

Hendrik Wagenseil

Data

Load and preview the data set.

y = "2022"
d = "03"

ifl = file.path(
  here::here()
  , y
  , "data"
  , paste0(y, "-day", d, "input")
)

txt = readLines(ifl)
head(txt, n = 20L)
 [1] "PnJJfVPBcfVnnPnBFFcggttrtgCrjDtSjzSS"            
 [2] "llWlLbmmdLLwLbqGdmTmbZfCQtzrMQfrjSzrtSrMqgMt"    
 [3] "sHlZTsWZwGGlZmGTmdlZbhJNRPphVfRvVnRBsRsJJV"      
 [4] "fsHtVbjtqstBghhwwPBw"                            
 [5] "SDQzzSzQrQMmmQlmlcNcJLZPgLrVZTdCddhgdPwwCw"      
 [6] "JmSWSVGGlJJbRsbpWHfbRj"                          
 [7] "tJndRtwtddPvllvfrldrfPpHWDgglFDWWmMmHWmHpZlS"    
 [8] "BBJTTjCsJWZCmSHSZD"                              
 [9] "LhqLcVzshTNjhqhcjLLTLjbTnGndfdwrfPRVRrdnwftQwJRv"
[10] "wHlPJZwbbZfqbFwqFZfrrcrJrtMWSMMVtVcJht"          
[11] "NzzzNBjNfLzvGfDNjMhVhrrMShLchsRVLs"              
[12] "DDdmmgBGDNdgfgZggnZbZHln"                        
[13] "jqNjZJqsGsRqJJqnlJJGzMzffcffTCfQcFmvcWfvTNfcvv"  
[14] "PdhVdrwphhVtDdSPLmFCWTLFWWTfFQQr"                
[15] "dSPwbbVdbpQllZMQbMjM"                            
[16] "QQdfflqvjTvfZqLMWfNDGhwsCNGGGM"                  
[17] "rzRRRTVTPTNhsDWDRhGC"                            
[18] "gHSTpTnppvjQgJjcql"                              
[19] "nzNvsFBBBFsNrnNBrvndfThwDbhVPzVVwhZZChpZPCbZ"    
[20] "GMQQStmcHHmlfMtPwbZVVVVhhPhbVc"                  

Part I

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.

Approach:

  • 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 in zip(c1, c2):
  i = set(i1) & set(i2)
  items.append(list(i)[0])

l = [ord(i) - 96 for i in items if i.islower()]
u = [ord(i) - 65 + 27 for i in items if i.isupper()]
sum(l + u)
7875

Part II

Approach:

  • arrange rucksacks in groups of 3
  • 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)
[1] 2479

Approach: