Advent of Code 2022 Day 1

Words
35
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 codes.
You can see all of them on GitHub

Part 1

inputs <- readLines('https://raw.githubusercontent.com/locharp/code-snippets/main/AdventOfCode/2022/inputs/1')
elves <- c()
elf <- c()
for (input in inputs) {
    if (input != '') {
        elf[length(elf)+1L] <- as.integer(input)
    } else {
        elves[length(elves)+1L] <- sum(elf)
        elf <- c()
    }
}
cat(max(elves),'\n')

Part 2

inputs <- readLines('https://raw.githubusercontent.com/locharp/code-snippets/main/AdventOfCode/2022/inputs/1')
elves <- c()
elf <- c()
for (input in inputs) {
    if (input != '') {
        elf[length(elf)+1L] = as.integer(input)
    } else {
        elves[length(elves)+1L] = sum(elf)
        elf <- c()
    }
}
cat(sum(sort(elves)[(length(elves)-2L):length(elves)]))
Advent of Code 2022 Day 1 | Ecency