Lochard avatar

Advent of Code 2022 Day 3

lochard

Published: 26 Jan 2023 › Updated: 26 Jan 2023Advent of Code 2022 Day 3

Advent of Code 2022 Day 3

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

Leave Advent of Code 2022 Day 3 to:

Written by

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

Read more #programming 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