-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday02.R
74 lines (50 loc) · 1.57 KB
/
day02.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
library(dplyr)
## PART 1 ----------------------------------------------------------------------
# Code:
# X for Rock, Y for Paper, and Z for Scissors
# A for Rock, B for Paper, and C for Scissors
# Score:
# 1 for Rock, 2 for Paper, and 3 for Scissors
day2 <- read.table("inputs/day02.txt")
tic <- Sys.time()
names(day2) <- c("elf", "me")
day2$numeric_elf <- as.numeric(factor(day2$elf))
day2$numeric_me <- as.numeric(factor(day2$me))
day2 <-
day2 %>%
mutate(
outcome = case_when(
numeric_elf == numeric_me ~ 3, # draw
numeric_elf == 3 & numeric_me == 1 ~ 6, # win
numeric_elf == 1 & numeric_me == 3 ~ 0, # loss
numeric_elf < numeric_me ~ 6, # win
numeric_elf > numeric_me ~ 0 # loss
)
)
day2$score <- day2$numeric_me + day2$outcome
sum(day2$score)
# 12156
Sys.time() - tic
## PART 2 ----------------------------------------------------------------------
rm(list = ls())
day2 <- read.table("inputs/day02.txt")
tic <- Sys.time()
names(day2) <- c("elf", "target_outcome")
day2$n_elf <- as.numeric(factor(day2$elf))
day2$n_target_outcome <- as.numeric(factor(day2$target_outcome))
# X means you need to lose, Y means you need to end the round in a draw,
# and Z means you need to win
day2 <-
day2 %>%
mutate(
n_me = case_when(
n_target_outcome == 2 ~ n_elf,
n_target_outcome == 3 ~ ifelse(n_elf == 3, 1, n_elf + 1),
n_target_outcome == 1 ~ ifelse(n_elf == 1, 3, n_elf - 1)
)
)
day2$outcome <- (day2$n_target_outcome - 1) * 3
day2$score <- day2$n_me + day2$outcome
sum(day2$score)
# 10835
Sys.time() - tic