04: High-Entropy Passphrases

library(mistlecode)
To install `mistlecode` yourself, run `devtools::install_github('guslipkin/mistlecode')`.

 Also loading:  cipheR data.table dplyr purrr slider stringr tidyverse glue

Part 1

Easy enough. I wanted to do it in one pipe-line, but couldn’t think of anything efficient enough.

"input.txt" |>
  readLines() |>
  map_lgl(\(x) {
    x <-
      x |>
      str_split_1(" ") |>
      table()
    all(x == 1)
  }) |>
  sum()
[1] 477

Part 2

I thought this would be harder, but then I realized I can just sort each string then use table again.

"input.txt" |>
  readLines() |>
  map_lgl(\(x) {
    x <-
      x |>
      str_split_1(" ") |>
      map_chr(\(y) {
        y |>
          str_split_1("") |>
          sort() |>
          paste0(collapse = "")
      }) |>
      table()
    all(x == 1)
  }) |>
  sum()
[1] 167