-
Notifications
You must be signed in to change notification settings - Fork 1
/
day02.Rmd
51 lines (45 loc) · 1.41 KB
/
day02.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
---
title: "--- Day 2: Password Philosophy ---"
author: Fleur Kelpin
date: Dec 2, 2020
output: github_document
---
```{r message=FALSE, warning=FALSE}
library(tidyverse)
input <- read_csv("day02.txt",
col_names = "line",
col_types = cols(line = col_character())
) %>%
extract(line,
"(\\d+)-(\\d+) (\\w): (\\w+)",
into = c("min", "max", "char", "pwd"),
convert = TRUE
)
input
```
# Part 1
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.
**How many passwords are valid** according to their policies?
```{r}
input %>%
mutate(occurs = str_count(pwd, char)) %>%
filter(occurs %>% between(min, max)) %>%
count()
```
# Part 2
Each policy actually describes two positions in the password, where 1 means the
first character, 2 means the second character, and so on. (Be careful; Toboggan
Corporate Policies have no concept of "index zero"!) **Exactly one of these
positions** must contain the given letter. Other occurrences of the letter are
irrelevant for the purposes of policy enforcement.
```{r}
input %>%
mutate(first = str_sub(pwd, min, min)) %>%
mutate(second = str_sub(pwd, max, max)) %>%
filter(char == first | char == second) %>%
filter(first != second) %>%
count()
```