# Libraries
library(tidyverse)
# Read input from file
input <- read_lines("../input/day11.txt") |>
trimws() |>
discard(~ .x == "")Day 11
Advent of Code: Worked Solutions
Setup
Part 1
# Reformat input
items <- str_match(input, "Starting items:(.*)")[,2] |>
discard(is.na) |>
str_split(",") |>
map(parse_number)
operations <- input |>
keep(~ str_detect(.x, "Operation:")) |>
str_replace("Operation: new = ", "~ ") |>
str_replace_all("old", ".x") |>
map(~ rlang::as_function(as.formula(.x)))
div <- parse_number(keep(input, ~ str_detect(.x, "Test:")))
divt <- parse_number(keep(input, ~ str_detect(.x, "If true:")))
divf <- parse_number(keep(input, ~ str_detect(.x, "If false:")))
test <- pmap(
list(div, divt, divf),
~ function(x) if_else(x %% ..1 == 0, ..2 + 1, ..3 + 1)
)
num_monkeys <- length(input) / 6
compute_monkey_business <- function(num_rounds, worry_func) {
# Initialize
activity <- rep(0, num_monkeys)
# Perform the tosses
for (round in 1:num_rounds) {
for (monkey in 1:num_monkeys) {
for (item in items[[monkey]]) {
worry <- worry_func(operations[[monkey]](item))
toss <- test[[monkey]](worry)
items[[toss]] <- c(items[[toss]], worry)
}
activity[[monkey]] <- activity[[monkey]] + length(items[[monkey]])
items[[monkey]] <- numeric(0)
}
}
# Compute monkey business score
activity |>
sort() |>
tail(2) |>
reduce(`*`)
}compute_monkey_business(num_rounds = 20, worry_func = \(x) floor(x / 3))Part 2
lcm <- DescTools::LCM(div)
compute_monkey_business(num_rounds = 10000, worry_func = \(x) x %% lcm)