2021-01: Sonar Sweep

library(tidyverse)
library(data.table)
dt <- data.table(read.table("input.txt", sep = "\n"))
head(dt)
      V1
   <int>
1:   191
2:   192
3:   201
4:   205
5:   206
6:   203

Part 1

dt$diff <- c(0, diff(dt$V1))
dt %>%
  filter(diff > 0) %>%
  nrow()
[1] 1374

Part 2

dt$diff <- c(0, 0, 0, diff(dt$V1, lag = 3))
dt %>%
  filter(diff > 0) %>%
  nrow()
[1] 1418

Speed Edition

Fastest Solution

# fastest solution
# data.table was faster until I turned on warn=FALSE in readLines for Base R
dt <- as.numeric(readLines("input.txt", warn = FALSE))
# part 1
sum(diff(dt, lag = 1) > 0)
[1] 1374
# part 2
sum(diff(dt, lag = 3) > 0)
[1] 1418

Benchmarks

rbenchmark::benchmark(
  "First try" = {
    library(tidyverse)
    library(data.table)
    dt <- data.table(read.table("input.txt", sep = "\n"))
    # part 1
    dt$diff <- c(0, diff(dt$V1))
    dt %>% filter(diff > 0) %>% nrow()
    # part 2
    dt$diff <- c(0, 0, 0, diff(dt$V1, lag = 3))
    dt %>% filter(diff > 0) %>% nrow()
    },
  "Base R" = {
    dt <- as.numeric(readLines("input.txt", warn = FALSE))
    # part 1
    sum(diff(dt, lag = 1) > 0)
    # part 2
    sum(diff(dt, lag = 3) > 0)
    },
  "data.table" = {
    # part 1 and 2
    data.table::fread("input.txt", 
                      sep = "\n")[, .(diff1 = sum(diff(V1, lag = 1) > 0),
                                      diff3 = sum(diff(V1, lag = 3) > 0))]
  },
  replications = 100, columns = c(1:5), order = "user.self")
        test replications user.self sys.self elapsed
2     Base R          100     0.046    0.008   0.057
3 data.table          100     0.149    0.011   0.187
1  First try          100     0.592    0.031   0.866