R

data.table - foverlap joins using numeric dates

Hi, in this post we are going to take a look at the foverlap function of the amazing data.table package. We start by running the examples, which are provided in the package documentation: https://search.r-project.org/CRAN/refmans/data.table/html/foverlaps.html Afterwards we are moving to an example, where we have date columns in the two data.table´s which we want to join based on overlaps. We generate the data ourselves, but the workflow is applicable to real world problems, once we understand the logic.

Advent of Code 2021 - Day 11

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:

Advent of Code 2021 - Day 13

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.

Advent of Code 2021 - Day 5

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:

Advent of Code 2021 - Day 7

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:

Advent of Code 2021 - Day 9

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.

Advent of Code 2021- Day 8

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:

Advent of Code 2021 - Day 4

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.

Advent of Code 2021 - Day 1

Hi, welcome to my solutions to the challenges of Advent of Code 2021. I present my solutions as a short post for every daily challenge. If you do not know what is going on, please go ahead and check out https://adventofcode.com/. I will not go into detail about the problem for day one, since you can simply visit https://adventofcode.com/2021/day/1. The goal for this year is to solve every puzzle.