library(tidyverse)
2020: Day 2
Setup
Part 1
Part 2
Part 1: Number of letters
We need to find how many passwords are valid according to their policy. The policies and passwords are given as follows:
1-3 a: abcde
1-3 b: cdefg
2-9 c: ccccccccc
Each line gives the password policy and then the password. The password policy indicates the lowest and highest number of times a given letter must appear for the password to be valid. For example, 1-3 a
means that the password must contain a
at least 1 time and at most 3 times.
First load the libraries we’ll need. We then read in the data and use tidyr
functions to separate out the parts of the policy and the password, making sure to convert the columns to numeric as appropriate:
<-
passwords read_tsv(here::here("2020", "day", "2", "input"),
col_names = FALSE) %>%
separate(X1, c("policy", "password"), sep = ":") %>%
separate(policy, c("count", "letter"), sep = " ") %>%
separate(count, c("min", "max")) %>%
mutate(min = as.integer(min),
max = as.integer(max))
Next, we use the stringr
function str_count()
to count how many times the given letter appears in the password, and conditional logic to check whether it is repeated within the specified number of times. Because TRUE
has a numeric value of 1 and FALSE
has a numeric value of 0, we can sum the resulting column to get a count of how many passwords are valid according to their policies.
%>%
passwords mutate(count = str_count(password, letter)) %>%
mutate(password_in_policy = if_else(
>= min & count <= max, TRUE, FALSE)) %>%
count summarise(correct = sum(password_in_policy)) %>%
pull(correct)
[1] 625
Part 2
Now the policy is interpreted differently. Each policy actually describes two positions in the password, where 1 means the first character, 2 means the second character, and so on. Exactly one of these positions must contain the given letter. How many are valid now?
There were a couple of gotchas here. When I used separate()
in the previous part, I had inadvertently left a leading whitespace in front of the password, something that was messing up my indexing with str_sub
. Using str_trim()
first cleared that up. Also, we need exactly one of the positions to match. |
is an inclusive or. We need xor()
for exclusive or instead.
%>%
passwords mutate(password = str_trim(password)) %>%
mutate(pos1_letter = str_sub(password, min, min),
pos2_letter = str_sub(password, max, max)) %>%
mutate(match_one = xor(pos1_letter == letter,
== letter)) %>%
pos2_letter summarise(correct = sum(match_one)) %>%
pull(correct)
[1] 391
Session info
Toggle
─ Session info ───────────────────────────────────────────────────────────────
setting value
version R version 4.3.1 (2023-06-16)
os macOS Sonoma 14.0
system aarch64, darwin20
ui X11
language (EN)
collate en_US.UTF-8
ctype en_US.UTF-8
tz Europe/London
date 2023-11-06
pandoc 3.1.1 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/ (via rmarkdown)
quarto 1.4.466 @ /usr/local/bin/quarto
─ Packages ───────────────────────────────────────────────────────────────────
package * version date (UTC) lib source
dplyr * 1.1.2 2023-04-20 [1] CRAN (R 4.3.0)
forcats * 1.0.0 2023-01-29 [1] CRAN (R 4.3.0)
ggplot2 * 3.4.2 2023-04-03 [1] CRAN (R 4.3.0)
lubridate * 1.9.2 2023-02-10 [1] CRAN (R 4.3.0)
purrr * 1.0.1 2023-01-10 [1] CRAN (R 4.3.0)
readr * 2.1.4 2023-02-10 [1] CRAN (R 4.3.0)
sessioninfo * 1.2.2 2021-12-06 [1] CRAN (R 4.3.0)
stringr * 1.5.0 2022-12-02 [1] CRAN (R 4.3.0)
tibble * 3.2.1 2023-03-20 [1] CRAN (R 4.3.0)
tidyr * 1.3.0 2023-01-24 [1] CRAN (R 4.3.0)
tidyverse * 2.0.0 2023-02-22 [1] CRAN (R 4.3.0)
[1] /Users/ellakaye/Library/R/arm64/4.3/library
[2] /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library
──────────────────────────────────────────────────────────────────────────────