Solution day 2 part 2.

This commit is contained in:
Bill Thiede 2020-12-02 19:38:36 -08:00
parent 17ea4c96c2
commit 0012d414b4

View File

@ -17,6 +17,20 @@
//! In the above example, 2 passwords are valid. The middle password, cdefg, is not; it contains no instances of b, but needs at least 1. The first and third passwords are valid: they contain one a or nine c, both within the limits of their respective policies.
//!
//! How many passwords are valid according to their policies?
//!
//! --- Part Two ---
//! While it appears you validated the passwords correctly, they don't seem to be what the Official Toboggan Corporate Authentication System is expecting.
//!
//! The shopkeeper suddenly realizes that he just accidentally explained the password policy rules from his old job at the sled rental place down the street! The Official Toboggan Corporate Policy actually works a little differently.
//!
//! 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.
//!
//! Given the same example list from above:
//!
//! 1-3 a: abcde is valid: position 1 contains a and position 3 does not.
//! 1-3 b: cdefg is invalid: neither position 1 nor position 3 contains b.
//! 2-9 c: ccccccccc is invalid: both position 2 and position 9 contain c.
//! How many passwords are valid according to the new interpretation of the policies?
use aoc_runner_derive::{aoc, aoc_generator};
use regex::Regex;
@ -47,14 +61,29 @@ fn parse(input: &str) -> Vec<Policy> {
.collect()
}
fn is_valid_policy(p: &Policy) -> bool {
fn is_valid_policy_part1(p: &Policy) -> bool {
let c = p.password.matches(&p.letter).count();
p.min <= c && c <= p.max
}
#[aoc(day2, part1)]
fn valid_policy_count(policies: &[Policy]) -> usize {
policies.iter().filter(|p| is_valid_policy(p)).count()
fn valid_policy_count_part1(policies: &[Policy]) -> usize {
policies.iter().filter(|p| is_valid_policy_part1(p)).count()
}
fn is_valid_policy_part2(p: &Policy) -> bool {
let letter = Some(p.letter.as_str());
// Password system uses it 1 based numbering, so we -1 to get zero based.
let first = p.password.get(p.min - 1..p.min);
let second = p.password.get(p.max - 1..p.max);
let valid = (first == letter) ^ (second == letter);
//dbg!((&p.password, &first, &second, &letter, &valid));
valid
}
#[aoc(day2, part2)]
fn valid_policy_count_part2(policies: &[Policy]) -> usize {
policies.iter().filter(|p| is_valid_policy_part2(p)).count()
}
#[cfg(test)]
@ -94,7 +123,12 @@ mod tests {
}
#[test]
fn validate_count() {
assert_eq!(valid_policy_count(&parse(INPUT)), 2);
fn validate_count_part1() {
assert_eq!(valid_policy_count_part1(&parse(INPUT)), 2);
}
#[test]
fn validate_count_part2() {
assert_eq!(valid_policy_count_part2(&parse(INPUT)), 1);
}
}