Aloha, it turns out that a dumbo octupus can flash. This helps us navigating through the dark. More on https://adventofcode.com/2021/day/11.
Data
raw_data <- readLines(con = "day_eleven") integer_split <- lapply(raw_data, function(x){ as.integer(strsplit(x, split = "")[[1]]) }) m <- do.call(rbind, integer_split) #preparing part 2 all_flashed <- ncol(m) * nrow(m) m_t <- cbind(NA, rbind(NA, m, NA), NA) Functions
MNeighbors <- function(mat, i = 2L, j = 3L, range_vec = -1 : 1) { mat[i + range_vec, j + range_vec] <- mat[i + range_vec, j + range_vec] + 1L mat[c(1, nrow(mat)),] <- NA mat[,c(1, ncol(mat))] <- NA return(mat) } Part 1:
Aloha, today we are going to fold up the manual, which allows us to use the thermal device.
#manual mat <- as.matrix(read.csv(file = "day_thirteen_paper", header = FALSE)) dot_mat <- matrix(data = NA, ncol = max(mat[, 1] + 1), nrow = max(mat[, 2] + 1)) dot_mat[cbind(mat[, 2] + 1 , mat[, 1] + 1)] <- 1 #instructions instructions <- read.csv(file = "day_thirteen_instructions", sep = " ", header = FALSE) instructions <- instructions[["V3"]] instructions <- strsplit(instructions, split = "=") instructions <- lapply(instructions, function(x) { return(data.
Aloha, seems like hydrothermal vents are a problem for our submarine on Day 5. Let us avoid them.
string_vec <- readLines(con = "day_five_one") string_vec <- unlist(strsplit(string_vec, split = "->")) string_vec <- trimws(string_vec) string_vec <- as.integer(unlist(strsplit(string_vec, split = ","))) matrix_vec <- matrix(data = string_vec, ncol = 4, byrow = TRUE) matrix_vec <- matrix_vec[(matrix_vec[, 1] == matrix_vec[, 3]) | (matrix_vec[, 2] == matrix_vec[, 4]),] Part 1:
ini_mat <- matrix(0L, nrow = 1000, ncol = 1000) for(i in 1 : nrow(matrix_vec)){ x_ind <- matrix_vec[i, 1] : matrix_vec[i, 3] y_ind <- matrix_vec[i, 2] : matrix_vec[i, 4] ini_mat[x_ind, y_ind] <- ini_mat[x_ind, y_ind] + 1 } (answer <- length(ini_mat[ini_mat > 1])) ## [1] 7473 Part 2:
Aloha, who heard of lanternfish before? Me neither.
Part 1:
initial_state <- readLines(con = "day_six_one") initial_state <- as.integer(unlist(strsplit(initial_state, split =","))) sim_fish_faster <- function(ini_state = c(3L, 4L, 3L, 1L, 2L), n_run = 18L, vec_length = 1e4) { m <- 1L fish_vec <- rep(NA_integer_, vec_length) fish_vec[1 : length(ini_state)] <- ini_state update_max <- min(which(is.na(fish_vec))) while(m <= n_run) { fish_vec <- fish_vec - 1L new_fish_n <- sum(fish_vec < 0L, na.rm = TRUE) fish_vec[fish_vec < 0L] <- 6L if(new_fish_n > 0) { fish_vec[update_max: (new_fish_n + update_max - 1L)] <- 8L update_max <- min(which(is.
Aloha, crabs can build and drive submarines. They need fuel though.
crab_pos <- readLines(con = "day_seven_one") crab_pos <- as.integer(unlist(strsplit(crab_pos, split =","))) Function:
FuelCon <- function(crab_pos) { align_pos <- min(crab_pos): max(crab_pos) fuel_sum <- sapply(align_pos, function(x){ diff_pos <- abs(x - crab_pos) wrong_fuel <- sum(diff_pos) real_fuel <- sum(sapply(diff_pos[diff_pos > 0], function(y) sum(1 : y))) list(wrong_fuel, real_fuel) }) return(fuel_sum) } Part 1:
fuel_con <- FuelCon(crab_pos) (answer <- min(unlist((fuel_con[1, ])))) ## [1] 356958 Part 2:
Aloha, seems like we are stuck in a lava cave.
raw_data <- readLines(con = "day_nine") integer_split <- lapply(raw_data, function(x){ as.integer(strsplit(x, split = "")[[1]]) }) Part 1:
m <- do.call(rbind, integer_split) #add NA m_t <- cbind(NA, rbind(NA, m, NA), NA) #functions MNeighbors <- function(mat, i = 2L, j = 3L) { all(mat[i, j] < c(mat[i, j - c(-1L, 1L)], mat[i - c(-1L, 1L), j]), na.rm = TRUE) } vMNeighbors <- Vectorize(FUN = MNeighbors, "j") #get borders rows <- 2L : (nrow(m_t) - 1L) cols <- 2L : (ncol(m_t) - 1L) mask <- lapply(rows, function(y) { vMNeighbors(m_t, i = y, j = cols) }) mask <- do.
Aloha, the submarine is broken. At least our seven digit display.
raw_data <- readLines(con = "day_eight_one") ## Warning in readLines(con = "day_eight_one"): unvollständige letzte Zeile in ## 'day_eight_one' gefunden raw_data <- strsplit(raw_data, split = "[|]") Part 1:
output_signals <- lapply(raw_data, function(x) { unlist(strsplit(x[2], split = " "))[-1] }) # 1, 4, 7, 8 signals count_chars <- lapply(output_signals, function(x){ sum(nchar(x) %in% c(2, 3, 4, 7)) }) (answer <- sum(unlist(count_chars))) ## [1] 504 Part 2:
Aloha, welcome to fourth day of Advent of Code 2021. Playing Bingo against a giant squid turned out to be more of struggle than I anticipated. I wanted to avoid loops for this puzzle and ended with a solution, which is a little bit hard to understand. Nevertheless, here it comes:
rand_n <- read.csv(file = "day_four_seq", header = FALSE, sep = ",") rand_seq <- as.integer(rand_n[1,]) #read the matrices mat_n <- lapply(1 : 100, function(x){ as.
Aloha, welcome to the third day of Advent of Code 2021. Again, I am going to solve this puzzle using Base R. Please see https://adventofcode.com/2021/day/3 for the problem description.
Part 1:
bin_data <- read.csv(file = "day_three", col.names = "bin", header = FALSE, colClasses = "character") head(bin_data) ## bin ## 1 110011110101 ## 2 110011100010 ## 3 010100011010 ## 4 011001100000 ## 5 010011011101 ## 6 011110111000 bin_m <- matrix(data = as.
Hi, welcome to the solution to day two of Advent of Code 2021. Please check out the puzzle at https://adventofcode.com/2021/day/2
# Data -------------------------------------------------------------------- dir_data <- read.csv(file = "day_two", col.names = c("direction", "value"), header = FALSE, sep = " ") head(dir_data) ## direction value ## 1 forward 9 ## 2 down 3 ## 3 down 8 ## 4 forward 2 ## 5 up 3 ## 6 forward 5 Part 1: