Advent of Code Day 2

Words
40
Reading
1 min
Listen
Play
4y

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

Advent of Code Day 2 | Ecency