
Director of data science. Mainly #rstats. Not really tweeting; now active at https://xitter.gq/t.co/LqQJ9dWupC
- Tweets2.4K
- Following170
- Followers354
- Likes4.5K
![input <- read.delim(text = readLines("input.txt"), sep = " ", col.names = c("do", "val"), header = FALSE) expand_cycles <- function(instruction) { if(instruction[1] == "noop") return(data.frame(do = "noop", val = 0)) rbind(data.frame(do = "addx", val = 0), instruction) } instr <- do.call(rbind, apply(input, 1, expand_cycles)) instr$current_val <- head(c(1, cumsum(instr$val) + 1), -1) # part 1 target_cycles <- seq(from = 20, to = 220, by = 40) sum(target_cycles * instr$current_val[target_cycles]) # part 2 instr$crt_pos <- rep(0:39, times = 6) instr$sprite_min <- instr$current_val - 1 instr$sprite_max <- instr$current_val + 1 instr$lit <- with(instr, ifelse(crt_pos <= sprite_max & crt_pos >= sprite_min, 1, 0)) image(matrix(instr$lit, nrow = 40, ncol = 6)[, 6:1])](https://pbs.twimg.com/media/FjrANKTXkAA9mHd.jpg?name=small&format=webp)
ALT input <- read.delim(text = readLines("input.txt"), sep = " ", col.names = c("do", "val"), header = FALSE) expand_cycles <- function(instruction) { if(instruction[1] == "noop") return(data.frame(do = "noop", val = 0)) rbind(data.frame(do = "addx", val = 0), instruction) } instr <- do.call(rbind, apply(input, 1, expand_cycles)) instr$current_val <- head(c(1, cumsum(instr$val) + 1), -1) # part 1 target_cycles <- seq(from = 20, to = 220, by = 40) sum(target_cycles * instr$current_val[target_cycles]) # part 2 instr$crt_pos <- rep(0:39, times = 6) instr$sprite_min <- instr$current_val - 1 instr$sprite_max <- instr$current_val + 1 instr$lit <- with(instr, ifelse(crt_pos <= sprite_max & crt_pos >= sprite_min, 1, 0)) image(matrix(instr$lit, nrow = 40, ncol = 6)[, 6:1])
![N <- 8 crates <- read.fwf("input.txt", widths = rep(4, 9), n = N) crates <- lapply(crates, function(col) gsub("[^A-Z]", "", col)) crates <- lapply(crates, function(vec) rev(vec[vec != ""])) moves <- read.table(text = gsub("move|from|to", ",", readLines("input.txt")), sep = ",", strip.white = TRUE, skip = N + 2, col.names = c("", "count", "from", "to")) move_crates <- function(crates, move, mover_fun) { from_stack <- crates[[move$from]] crates[[move$from]] <- head(from_stack, length(from_stack) - move$count) crates[[move$to]] <- c(crates[[move$to]], mover_fun(tail(from_stack, move$count))) crates } solve <- function(crates, moves, mover_fun) { for(i in 1:nrow(moves)) crates <- move_crates(crates, moves[i, ], mover_fun) paste(sapply(crates, tail, 1), collapse = "") } # part 1 solve(crates, moves, rev) # part 2 solve(crates, moves, identity)](https://pbs.twimg.com/media/FjQHSUkXwAAYwx4.jpg?name=small&format=webp)
ALT N <- 8 crates <- read.fwf("input.txt", widths = rep(4, 9), n = N) crates <- lapply(crates, function(col) gsub("[^A-Z]", "", col)) crates <- lapply(crates, function(vec) rev(vec[vec != ""])) moves <- read.table(text = gsub("move|from|to", ",", readLines("input.txt")), sep = ",", strip.white = TRUE, skip = N + 2, col.names = c("", "count", "from", "to")) move_crates <- function(crates, move, mover_fun) { from_stack <- crates[[move$from]] crates[[move$from]] <- head(from_stack, length(from_stack) - move$count) crates[[move$to]] <- c(crates[[move$to]], mover_fun(tail(from_stack, move$count))) crates } solve <- function(crates, moves, mover_fun) { for(i in 1:nrow(moves)) crates <- move_crates(crates, moves[i, ], mover_fun) paste(sapply(crates, tail, 1), collapse = "") } # part 1 solve(crates, moves, rev) # part 2 solve(crates, moves, identity)
![input <- read.delim("input.txt", sep = ",", header = FALSE) find_overlap <- function(row) { ranges <- lapply(strsplit(row, "-"), as.integer) sections <- lapply(ranges, function(r) seq(r[[1]], r[[2]], by = 1L)) overlap <- Reduce(intersect, sections) data.frame(len1 = length(sections[[1]]), len2 = length(sections[[2]]), len_shared = length(overlap)) } section_data <- do.call(rbind, apply(input, 1, find_overlap)) # part 1 with(section_data, sum(len1 == len_shared | len2 == len_shared)) # part 2 sum(section_data$len_shared != 0)](https://pbs.twimg.com/media/FjIzrsCXgAMmDp9.jpg?name=small&format=webp)
ALT input <- read.delim("input.txt", sep = ",", header = FALSE) find_overlap <- function(row) { ranges <- lapply(strsplit(row, "-"), as.integer) sections <- lapply(ranges, function(r) seq(r[[1]], r[[2]], by = 1L)) overlap <- Reduce(intersect, sections) data.frame(len1 = length(sections[[1]]), len2 = length(sections[[2]]), len_shared = length(overlap)) } section_data <- do.call(rbind, apply(input, 1, find_overlap)) # part 1 with(section_data, sum(len1 == len_shared | len2 == len_shared)) # part 2 sum(section_data$len_shared != 0)
![input <- data.frame(contents = readLines("input.txt")) input$size <- nchar(input$contents) / 2 input$cpt1 <- substr(input$contents, 1, input$size) input$cpt2 <- substr(input$contents, input$size + 1, input$size * 2) input$group <- rep((1:(nrow(input)/3)), each = 3) # helper find_common <- function(strings) Reduce(intersect, strsplit(strings, "")) # part 1 input$common <- apply(input[c("cpt1", "cpt2")], 1, find_common) sum(match(input$common, c(letters, LETTERS))) # part 2 badges <- aggregate(contents ~ group, input, find_common) sum(match(badges$contents, c(letters, LETTERS)))](https://pbs.twimg.com/media/FjCCuA2XkAEElFF.jpg?name=small&format=webp)
ALT input <- data.frame(contents = readLines("input.txt")) input$size <- nchar(input$contents) / 2 input$cpt1 <- substr(input$contents, 1, input$size) input$cpt2 <- substr(input$contents, input$size + 1, input$size * 2) input$group <- rep((1:(nrow(input)/3)), each = 3) # helper find_common <- function(strings) Reduce(intersect, strsplit(strings, "")) # part 1 input$common <- apply(input[c("cpt1", "cpt2")], 1, find_common) sum(match(input$common, c(letters, LETTERS))) # part 2 badges <- aggregate(contents ~ group, input, find_common) sum(match(badges$contents, c(letters, LETTERS)))
![input <- read.delim("input.txt", sep = " ", header = FALSE, col.names = c("them", "me")) # helpers lookup <- function(action, mat) mat[action[[1]], action[[2]]] # part 1 dimnames <- list(c("A", "B", "C"), c("X", "Y", "Z")) score <- matrix(c(1+3, 2+6, 3+0, 1+0, 2+3, 3+6, 1+6, 2+0, 3+3), byrow = TRUE, nrow = 3, dimnames = dimnames) input$score <- apply(input, 1, lookup, mat = score) sum(input$score) # part 2 shape <- matrix(c("Z", "X", "Y", "X", "Y", "Z", "Y", "Z", "X"), byrow = TRUE, nrow = 3, dimnames = dimnames) input$me <- apply(input, 1, lookup, mat = shape) input$score <- apply(input, 1, lookup, mat = score) sum(input$score)](https://pbs.twimg.com/media/Fi-4YuKXEAEwx-6.jpg?name=small&format=webp)
ALT input <- read.delim("input.txt", sep = " ", header = FALSE, col.names = c("them", "me")) # helpers lookup <- function(action, mat) mat[action[[1]], action[[2]]] # part 1 dimnames <- list(c("A", "B", "C"), c("X", "Y", "Z")) score <- matrix(c(1+3, 2+6, 3+0, 1+0, 2+3, 3+6, 1+6, 2+0, 3+3), byrow = TRUE, nrow = 3, dimnames = dimnames) input$score <- apply(input, 1, lookup, mat = score) sum(input$score) # part 2 shape <- matrix(c("Z", "X", "Y", "X", "Y", "Z", "Y", "Z", "X"), byrow = TRUE, nrow = 3, dimnames = dimnames) input$me <- apply(input, 1, lookup, mat = shape) input$score <- apply(input, 1, lookup, mat = score) sum(input$score)



![input <- strsplit(readLines("input.txt"), "")[[1]] find_marker <- function(input, chars) { marker <- FALSE i <- 0 while(!marker) { i <- i + 1 marker <- !anyDuplicated(input[i:(i + chars - 1)]) } return(i + chars - 1) } # part 1 find_marker(input, 4) # part 2 find_marker(input, 14)](https://pbs.twimg.com/media/FjRXu_aXEAELvaN.jpg?name=small&format=webp)





