DEV Community

Discussion on: Advent of Code 2020 Solution Megathread - Day 2: Password Philosophy

Collapse
 
ballpointcarrot profile image
Christopher Kruse

Rust solution for Day 2. Felt happy and clever at the inclusion of the XOR for part 2.

Full repo on Github.

use aoc_runner_derive::{aoc, aoc_generator};
use regex::Regex;

struct PasswordChallenge {
    password: String,
    min: usize,
    max: usize,
    search_char: char,
}

#[aoc_generator(day2)]
fn parse_input_day1(input: &str) -> Vec<PasswordChallenge> {
    input.lines().map(|v| parse_line(v)).collect()
}

fn parse_line(line: &str) -> PasswordChallenge {
    let pattern = Regex::new(r"^(\d+)-(\d+)\s(\w):\s(\w+)$").expect("couldn't create Regex");
    let captures = pattern.captures(line).unwrap();

    PasswordChallenge {
        min: str::parse(captures.get(1).unwrap().as_str()).unwrap(),
        max: str::parse(captures.get(2).unwrap().as_str()).unwrap(),
        search_char: (captures.get(3).unwrap().as_str()).chars().next().unwrap(),
        password: String::from(captures.get(4).unwrap().as_str()),
    }
}

#[aoc(day2, part1)]
fn valid_password_count(input: &Vec<PasswordChallenge>) -> usize {
    input
        .iter()
        .filter(|challenge| is_valid_password_by_char_count(challenge))
        .count()
}

fn is_valid_password_by_char_count(challenge: &PasswordChallenge) -> bool {
    let pw_chars_count = challenge
        .password
        .chars()
        .filter(|ch| ch == &challenge.search_char)
        .count();

    challenge.min <= pw_chars_count && challenge.max >= pw_chars_count
}

#[aoc(day2, part2)]
fn valid_password_positions(input: &Vec<PasswordChallenge>) -> usize {
    input
        .iter()
        .filter(|challenge| is_valid_password_by_pos(challenge))
        .count()
}

fn is_valid_password_by_pos(challenge: &PasswordChallenge) -> bool {
    challenge.password.char_indices().fold(false, |memo, (idx, ch)| {
        if idx == challenge.min-1 || idx == challenge.max-1 {
            memo ^ (ch == challenge.search_char)
        } else {
            memo
        }
    })
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
patryk profile image
Patryk Woziński

Rust looks intriguing! I hope that one day I will find time to learn this language. :D