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$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 Rdt <-as.numeric(readLines("input.txt", warn =FALSE))# part 1sum(diff(dt, lag =1) >0)
[1] 1374
# part 2sum(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 1sum(diff(dt, lag =1) >0)# part 2sum(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