Data
Load and preview the data set.
y = "2022"
d = "02"
ifl = file.path(
here::here()
, y
, "data"
, paste0(y, "-day", d, "input")
)
txt = readLines(ifl)
head(txt, n = 30L)
[1] "C Z" "A Y" "C Z" "A Y" "C Y" "A Z" "B Y" "C X" "A Y" "B X" "B X" "A Y"
[13] "C Z" "C Y" "C Y" "A Y" "C Y" "B X" "B X" "A Z" "C Y" "B Z" "C Z" "A Z"
[25] "B Y" "A Y" "C Z" "A Z" "A Y" "C Z"
Part I
Get the score of the rock paper scissors game.
- A = rock, B = paper, C = scissors
- X = rock, Y = paper, Z = scissors
Approach:
- define a named vector
pts
with all possible combinations and the resulting score
- match
txt
with pts
to get the score for all games and sum up
pts = c(
"A X" = 1 + 3
, "A Y" = 2 + 6
, "A Z" = 3 + 0
, "B X" = 1 + 0
, "B Y" = 2 + 3
, "B Z" = 3 + 6
, "C X" = 1 + 6
, "C Y" = 2 + 0
, "C Z" = 3 + 3
)
score = pts[match(txt, table = names(pts))]
sum(score)
Approach:
- define dictionary
pts
with all possible combinations and the resulting score
- use list comprehension to get the score per game and sum up
txt = r.txt
pts = {
'A X': 1 + 3
, 'A Y': 2 + 6
, 'A Z': 3 + 0
, 'B X': 1 + 0
, 'B Y': 2 + 3
, 'B Z': 3 + 6
, 'C X': 1 + 6
, 'C Y': 2 + 0
, 'C Z': 3 + 3
}
score = [pts[i] for i in txt]
sum(score)
Part II
Get the score of the rock paper scissors game.
- A = rock, B = paper, C = scissors
- X = loose, Y = draw, Z = win
Approach:
- same as in part I, just re-define vector
pts
pts = c(
"A X" = 3 + 0
, "A Y" = 1 + 3
, "A Z" = 2 + 6
, "B X" = 1 + 0
, "B Y" = 2 + 3
, "B Z" = 3 + 6
, "C X" = 2 + 0
, "C Y" = 3 + 3
, "C Z" = 1 + 6
)
score = pts[match(txt, table = names(pts))]
sum(score)
Approach:
- same as in part I, just re-define dictionary
pts
txt = r.txt
pts = {
'A X': 3 + 0
, 'A Y': 1 + 3
, 'A Z': 2 + 6
, 'B X': 1 + 0
, 'B Y': 2 + 3
, 'B Z': 3 + 6
, 'C X': 2 + 0
, 'C Y': 3 + 3
, 'C Z': 1 + 6
}
score = [pts[i] for i in txt]
sum(score)