Advent of Code 2018

2 minute read

I’m going to try to complete the Advent of Code again this year. I’ll put all the exercises I complete in this post.

Links to the puzzles are at https://adventofcode.com/2018

Day 1

library(tidyverse)


## part 1
readr::read_lines("advent-data/2018-12-01-day1.txt") %>%
  as.numeric() %>%
  sum()
## [1] 408
## part 2
input <- readr::read_lines("advent-data/2018-12-01-day1.txt") %>%
  as.numeric()

already_seen <- function(input) {

  i <- 1

  while (TRUE) {
    v_sum <- cumsum(rep(input, i))
    has_dup <- any(duplicated(v_sum))
    if (has_dup) {
      return(v_sum[which(duplicated(v_sum))[1]])
    }
    i <- i + 1
  }
  
}

already_seen(input)
## [1] 55250

Day 2

input <- readr::read_lines("advent-data/2018-12-02-day2.txt") 

## part 1
count_letters <- function(input) {

  n_letters <- strsplit(input, "") %>%
    purrr::map(table)
  
  has_2 <- function(x) {
    as.integer(any(x == 2))
  }
  has_3 <- function(x) {
    as.integer(any(x == 3))
  }

  has_2_vec <- purrr::map_int(n_letters, has_2)
  has_3_vec <- purrr::map_int(n_letters, has_3)

  sum(has_2_vec) * sum(has_3_vec)
}

count_letters(input)
## [1] 6000
## part 2
all_in <- crossing(in1 = input, in2 = input) %>%
  mutate(
    split1 = strsplit(in1, ""),
    split2 = strsplit(in2, ""),     
    n_diff = map2_int(split1, split2, ~ sum(.x != .y))    
  )

all_in %>%
  filter(n_diff == 1) %>%
  slice(1) %>%
  mutate(word = map2_chr(
    split1,
    split2,
    function(x, y) {
      x <- unlist(x)
      y <- unlist(y)
      paste(x[x == y], collapse = "")
    })) %>%
  pull(word)
## [1] "pbykrmjmizwhxlqnasfgtycdv"

Day 3

That’s far from the prettiest code I’ve written! But it gets the job done.

extract_coords <- function(input) {

  readr::read_delim(
    input, delim = " ", col_names = FALSE
  ) %>%
    tidyr::extract(X1, into = "id", regexp = "([[:digit]]+)") %>%
    tidyr::extract(X3, into = c("x_begin", "y_begin"), regex = "([[:digit:]]+),([[:digit:]]+):") %>%
    tidyr::extract(X4, into = c("width", "height"), regex = "([[:digit:]]+)x([[:digit:]]+)") %>%
    dplyr::select(-X2) %>%
    dplyr::mutate_all(as.numeric)
  
}


find_total_dim <- function(coords) {
  res <- coords %>%
    dplyr::mutate(total_width = x_begin + width,
                  total_height = y_begin + height)

  c(total_width = max(res$total_width),
    total_height = max(res$total_height))
  
}


fill_matrix <- function(input) {

  c <- extract_coords(input)
  m_dim <- find_total_dim(c)

  M <- matrix(0,
              nrow = m_dim[1],
              ncol = m_dim[2])


  for (i in seq_len(nrow(c))) {
    i_s <- (c$x_begin[i] + 1):(c$x_begin[i] + c$width[i])
    j_s <- (c$y_begin[i] + 1):(c$y_begin[i] + c$height[i])
    M[i_s, j_s] <- M[i_s, j_s] + 1
  }

  M
  
}


more_two_claims <- function(input) {
  M <- fill_matrix(input)
  sum(M >= 2) 
}

## part 1 answer
more_two_claims("advent-data/2018-12-03-day3.txt")
## Parsed with column specification:
## cols(
##   X1 = col_character(),
##   X2 = col_character(),
##   X3 = col_character(),
##   X4 = col_character()
## )
## [1] 109716
overlaps <- function(x_begin, y_begin, width, height, M) {
  i <- (x_begin + 1):(x_begin + width)
  j <- (y_begin + 1):(y_begin + height)
  all(M[i, j] == 1) 
}

no_overlap <- function(input) {
  M <- fill_matrix(input)

  c <- extract_coords(input)
  res <- logical(nrow(c))
  
  for (i in seq_len(nrow(c))) {
    res[i] <- overlaps(c$x_begin[i], c$y_begin[i],
                       c$width[i], c$height[i], M)
  }

  c$id[res]
  
}

## part 2 answer
no_overlap("advent-data/2018-12-03-day3.txt")
## Parsed with column specification:
## cols(
##   X1 = col_character(),
##   X2 = col_character(),
##   X3 = col_character(),
##   X4 = col_character()
## )
## Parsed with column specification:
## cols(
##   X1 = col_character(),
##   X2 = col_character(),
##   X3 = col_character(),
##   X4 = col_character()
## )
## [1] 124

Comments