Advent of Code 2022 Day 3

Words
42
Reading
1 min
Listen
Play
4y

Sorry that I am blogging via my phone and I don't want to type too much.
I will just share the finished code.
You can see all of them on GitHub.


Advent of Code 2022
Day 3
Part 1

inputs <- read.table('https://raw.githubusercontent.com/locharp/code-snippets/main/AdventOfCode/2022/inputs/3', sep='\n')[[1L]]
sum <- 0L
for (i in seq(from=1L, to= length(inputs), by=3L)) {
    priority <- utf8ToInt(intersect(intersect(strsplit(inputs[i], '')[[1L]], strsplit(inputs[i+1L], '')[[1L]]), strsplit(inputs[i+2L], '')[[1L]]))
    sum <- sum + ifelse(priority > 96L, priority - 96L, priority - 38L)
}
sum

Part 2

inputs <- readLines('https://raw.githubusercontent.com/locharp/code-snippets/main/AdventOfCode/inputs/3')
sum <- 0
for (input in inputs) {
    len <- nchar(input)
    items <- strsplit(input, NULL)
    item <- intersect(items[[1]][1:(len/2)], items[[1]][(len/2+1):len])
    priority <- utf8ToInt(item)
    sum <- sum + ifelse(priority > 96, priority - 96, priority - 38)
}
sum

Advent of Code 2022 Day 3 | Ecency