Skip to content

Instantly share code, notes, and snippets.

@ryanrosenberg
Created April 8, 2018 22:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanrosenberg/6dafcff6cda15e36df5627f6e19ac64d to your computer and use it in GitHub Desktop.
Save ryanrosenberg/6dafcff6cda15e36df5627f6e19ac64d to your computer and use it in GitHub Desktop.
Solution for Riddler Express 4/8/18
library(tidyverse)
month_vec <- c(rep(1, 31),
rep(2, 28),
rep(3, 31),
rep(4, 30),
rep(5, 31),
rep(6, 30),
rep(7, 31),
rep(8, 31),
rep(9, 30),
rep(10, 31),
rep(11, 30),
rep(12, 31))
day_vec <- c(1:31,
1:28,
1:31,
1:30,
1:31,
1:30,
1:31,
1:31,
1:30,
1:31,
1:30,
1:31)
century_dates <- tibble(month = rep(month_vec, 99),
day = rep(day_vec, 99),
year = rep(1:99, each = 365)) %>%
bind_rows(tibble(month = 2,
day = 29,
year = subset(1:99, 1:99 %% 4 == 0))) %>%
mutate(vandalism = ifelse(month*day==year, 1, 0),
streak = 1) %>%
arrange(year, month, day)
sum(century_dates$vandalism)
century_dates %>%
group_by(year) %>%
summarize(year_vandalism = sum(vandalism)) %>%
arrange(-year_vandalism)
century_dates %>%
group_by(year) %>%
summarize(year_vandalism = sum(vandalism)) %>%
arrange(year_vandalism)
for (i in 2:length(century_dates$vandalism)){
century_dates$streak[i] <- ifelse(century_dates$vandalism[i] == century_dates$vandalism[i-1], century_dates$streak[i-1] + 1, 1)
}
max(century_dates$streak)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment