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 } }) }
Rust looks intriguing! I hope that one day I will find time to learn this language. :D
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Rust solution for Day 2. Felt happy and clever at the inclusion of the XOR for part 2.
Full repo on Github.
Rust looks intriguing! I hope that one day I will find time to learn this language. :D