|
| 1 | +//! Day 2: Password Philosophy |
| 2 | +//! |
| 3 | +//! ## Problem Description |
| 4 | +//! |
| 5 | +//! Part 1: Validate passwords based on character count policy - each line contains |
| 6 | +//! a range (min-max), a character, and a password. Count how many passwords |
| 7 | +//! have the character appear between min and max times (inclusive). |
| 8 | +//! |
| 9 | +//! Part 2: Validate passwords based on position policy - each line contains |
| 10 | +//! two positions (1-indexed), a character, and a password. Count how many |
| 11 | +//! passwords have the character appear in exactly one of the two positions. |
| 12 | +//! |
| 13 | +//! ## Solution Approach |
| 14 | +//! |
| 15 | +//! **Input Parsing**: Parses each line in format "min-max char: password" into: |
| 16 | +//! - Policy tuple: (min_position, max_position, character) |
| 17 | +//! - Password string |
| 18 | +//! |
| 19 | +//! **Part 1 Strategy**: Character frequency counting |
| 20 | +//! - For each password, count occurrences of the specified character |
| 21 | +//! - Check if count falls within the min-max range |
| 22 | +//! - Count valid passwords using iterator filters |
| 23 | +//! |
| 24 | +//! **Part 2 Strategy**: XOR position checking |
| 25 | +//! - Check if character appears at first position (min-1 for 0-indexing) |
| 26 | +//! - Check if character appears at second position (max-1 for 0-indexing) |
| 27 | +//! - Valid when exactly one position contains the character (XOR logic) |
| 28 | +//! - Count valid passwords using iterator filters |
| 29 | +//! |
| 30 | +//! **Parsing Notes**: Uses split on ['-', ' ', ':'] delimiters and careful indexing |
| 31 | +//! to extract policy components and password from each line. |
| 32 | +
|
1 | 33 | type Policy = (usize, usize, char); |
2 | 34 |
|
3 | 35 | fn parse_input(input: &str) -> Vec<(Policy, &str)> { |
|
0 commit comments