Lochard avatar

Advent of Code Day 2

lochard

Published: 24 Jan 2023 › Updated: 24 Jan 2023Advent of Code Day 2

Advent of Code Day 2

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
Part 1

inputs <- readLines('https://raw.githubusercontent.com/locharp/code-snippets/main/AdventOfCode/2022/inputs/2')
choices <- list('A'=1L, 'B'=2L, 'C'=3L,
                'X'=1L, 'Y'=2L, 'Z'=3L)
total <- 0L
for (input in inputs) {
    inp <- strsplit(input, " ")
    res <- choices[[inp[[1L]][2L]]] - choices[[inp[[1L]][1L]]]
    if (res == 1L | res == -2L) {
        total <- total + 6L
    } else if (res == 0L) {
        total <- total + 3L
    }
    total <- total + choices[[inp[[1L]][2L]]]
}

total

Part 2

inputs <- readLines('https://raw.githubusercontent.com/locharp/code-snippets/main/AdventOfCode/2022/inputs/2')
choices <- list('A'=1L, 'B'=2L, 'C'=3L,
                'X'=-1L, 'Y'=0L, 'Z'=1L)
total <- 0L
for (input in inputs) {
    inp <- strsplit(input, " ")
    res <- choices[[inp[[1L]][2L]]]
    choice <- choices[[inp[[1L]][1L]]] + res
    if (choice == 4L) {
        choice <- 1L
    } else if (choice == 0L) {
        choice <- 3L
    }
    res <- res * 3L + 3L
    total <- total + res + choice
}

total

Leave Advent of Code Day 2 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