2020-02: Password Philosophy

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()
df <- read.csv("input.csv")
head(df)
  min max letter             password
1   1  14      b  bbbbbbbbbbbbbbbbbbb
2   3  14      v      vvpvvvmvvvvvvvv
3   2   5      m              mfvxmmm
4  15  20      z zdzzzrjzzzdpzzdzzzzz
5   6   8      g           tggjggggrg
6   2   3      l                nlllw

Part 1

count <- 0

for(i in 1:nrow(df)) {
  if(str_count(df$password[i], as.character(df$letter[i])) >= df$min[i] & 
     str_count(df$password[i], as.character(df$letter[i])) <= df$max[i])
    count <- count + 1
}

count
[1] 548

Part 2

count <- 0

for(i in 1:nrow(df)) {
  sub <- paste(substr(df$password[i], df$min[i], df$min[i]), 
               substr(df$password[i], df$max[i], df$max[i]))
  if(str_count(sub, as.character(df$letter[i])) == 1)
    count <- count + 1
}

count
[1] 502