Advent of Code 2022: Programming Puzzle - Day 2
Melanjutkan posting saya sebelumnya. Kali ini saya hendak menyelesaikan puzzle hari kedua.
Day 2
Part One
Kita diberikan satu dataset sederhana dan disuruh mencari skor total pertandingan suit para elves.
Berikut adalah R script yang saya gunakan untuk menjawab:
# dimulai dari hati yang suci
rm(list=ls())
# libraries
library(dplyr)
library(tidyr)
# import data
df = readLines("input.txt")
# kita ubah ke data frame ya
df =
data.frame(dummy = df) %>%
separate(dummy,into = c("oppo","our"),sep = " ") %>%
# kita akan ubah ourself menjadi ABC
mutate(our = case_when(
our == "X" ~ "A",
our == "Y" ~ "B",
our == "Z" ~ "C"
)) %>%
# hitung skor dari hand sign
mutate(skor_1 = case_when(
our == "A" ~ 1,
our == "B" ~ 2,
our == "C" ~ 3
)) %>%
# menentukan menang atau tidak
mutate(status = case_when(
# A for Rock, B for Paper, and C for Scissors
our == oppo ~ "Draw",
our == "A" & oppo == "C" ~ "Win",
our == "B" & oppo == "A" ~ "Win",
our == "C" & oppo == "B" ~ "Win"
)) %>%
mutate(status = ifelse(is.na(status),"Lost",status)) %>%
# hitung skor hasil pertandingan
mutate(skor_2 = case_when(
# 0 if you lost, 3 if the round was a draw, and 6 if you won
status == "Lost" ~ 0,
status == "Draw" ~ 3,
status == "Win" ~ 6
)) %>%
mutate(skor_total = skor_1 + skor_2)
# moment of truth
sum(df$skor_total)
Saya menggunakan prinsip tidy pada dataframe untuk memanipulasi baris dan kolom.
Berikut adalah video yang saat saya menyelesaikan puzzle-nya:
Part Two
Hal yang menarik terjadi pada bagian kedua ini. Ternyata arti dari X
, Y
, dan Z
berbeda. Saya akan buat ulang R script-nya menjadi berikut:
# dimulai dari hati yang suci
rm(list=ls())
# libraries
library(dplyr)
library(tidyr)
# import data
df = readLines("input.txt")
# perubahan yang terjadi:
# X means you need to lose,
# Y means you need to end the round in a draw,
# and Z means you need to win.
# kita ubah ke data frame ya
df =
data.frame(dummy = df) %>%
separate(dummy,into = c("oppo","status"),sep = " ") %>%
# kita akan ubah status sesuai dengan perkataan elf
mutate(status = case_when(
status == "X" ~ "lost",
status == "Y" ~ "draw",
status == "Z" ~ "win"
)) %>%
mutate(our = case_when(
status == "draw" ~ oppo,
oppo == "A" & status == "lost" ~ "C", # scissor
oppo == "A" & status == "win" ~ "B", # paper
oppo == "B" & status == "lost" ~ "A", # rock
oppo == "B" & status == "win" ~ "C", # scissor
oppo == "C" & status == "lost" ~ "B", # paper
oppo == "C" & status == "win" ~ "A" # rock
)) %>%
# hitung skor dari hand sign
mutate(skor_1 = case_when(
our == "A" ~ 1,
our == "B" ~ 2,
our == "C" ~ 3
)) %>%
# menentukan menang atau tidak
mutate(status = case_when(
# A for Rock, B for Paper, and C for Scissors
our == oppo ~ "Draw",
our == "A" & oppo == "C" ~ "Win",
our == "B" & oppo == "A" ~ "Win",
our == "C" & oppo == "B" ~ "Win"
)) %>%
mutate(status = ifelse(is.na(status),"Lost",status)) %>%
# hitung skor hasil pertandingan
mutate(skor_2 = case_when(
# 0 if you lost, 3 if the round was a draw, and 6 if you won
status == "Lost" ~ 0,
status == "Draw" ~ 3,
status == "Win" ~ 6
)) %>%
mutate(skor_total = skor_1 + skor_2)
# moment of truth
sum(df$skor_total)
Berikut adalah video yang saat saya menyelesaikan puzzle-nya: