Lochard avatar

Advent of Code 2022 Day 1

lochard

Published: 23 Jan 2023 › Updated: 23 Jan 2023Advent of Code 2022 Day 1

Advent of Code 2022 Day 1

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)]))

Leave Advent of Code 2022 Day 1 to:

Written by

Asylum seeker who hopes to be rid of authoritarian and their surveillance.

Read more #adventofcode posts


Best Posts From Lochard

We have not curated any of lochard's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From Lochard