Advent of Code 2022 Day 1

Advent of Code 2022 Day 1

By Lochard | As a newbie programmer | 24 Jan 2023



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 1

**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()
    }
}
max(elves)

**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()
    }
}
sum(sort(elves)[(length(elves)-2L):length(elves)])

How do you rate this article?

2


Lochard
Lochard

20240228 Arrived in the UK for about 2 week. All my fears persist. Some are even getting worse. https://github.com/locharp/asylum_diary/


As a newbie programmer
As a newbie programmer

Sharing entry level codes. My snippets on GitHub https://github.com/locharp/code-snippets

Publish0x

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.