Advent of Code 2022 Day 3

Advent of Code 2022 Day 3

By Lochard | As a newbie programmer | 27 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 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

 

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.