02: Cube Conundrum
Part 1
I’m pretty pleased overall. I got a bit hung up on the if statements because y
was originally all the colors, not one color at a time so I had to add the second (actually third) map
statement.
red <- 12; green <- 13; blue <- 14;
dt |>
purrr::map_lgl(\(x) {
x <-
x |>
purrr::map(\(x2) {
x2 |>
stringr::str_split(" ") |>
purrr::map(\(y) {
r <- 1
if ((y[2] == 'red' & as.numeric(y[1]) > red) |
(y[2] == 'green' & as.numeric(y[1]) > green) |
(y[2] == 'blue' & as.numeric(y[1]) > blue)) {
r <- 0
}
return(r)
})
}) |>
unlist()
all(x == 1)
}) |>
which() |>
sum()
[1] 2006
Part 2
Pretty happy with this too. I got pretty stuck on X1
being character because I was casting later but that meant I was finding the max of a character not a numeric. Moved the cast and it works like a dream.