2021-01: Sonar Sweep

library(tidyverse)
── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
✔ ggplot2 3.4.0      ✔ purrr   0.3.5 
✔ tibble  3.1.8      ✔ dplyr   1.0.10
✔ tidyr   1.2.1      ✔ stringr 1.4.1 
✔ readr   2.1.3      ✔ forcats 0.5.2 
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
library(data.table)

Attaching package: 'data.table'

The following objects are masked from 'package:dplyr':

    between, first, last

The following object is masked from 'package:purrr':

    transpose
dt <- data.table(read.table("input.txt", sep = "\n"))
head(dt)
    V1
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.023    0.004   0.027
3 data.table          100     0.048    0.005   0.055
1  First try          100     0.426    0.017   0.444