Advent of Code 2022 Day 2

Advent of Code 2022 Day 2

By Lochard | As a newbie programmer | 25 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](https://github.com/locharp/code-snippets/tree/main/AdventOfCode).

---

Advent of Code 2022

Day 2

**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

 

How do you rate this article?

1


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.