diff options
| author | omagdy <omar.professional8777@gmail.com> | 2024-12-08 15:28:08 +0200 |
|---|---|---|
| committer | omagdy <omar.professional8777@gmail.com> | 2024-12-08 15:28:08 +0200 |
| commit | 01c6b2cb42a4144a573ec43e9e25c6718741a272 (patch) | |
| tree | 158b76f6363cd29251cedfa33f0cbf83ea5229c8 /2020/Rust/src/day2.rs | |
| parent | 57fc57ecc40f879a3ccb637a5f420a1e0a655eab (diff) | |
| download | aoc-01c6b2cb42a4144a573ec43e9e25c6718741a272.tar.xz aoc-01c6b2cb42a4144a573ec43e9e25c6718741a272.zip | |
Some solutions to AOC 2020
Diffstat (limited to '2020/Rust/src/day2.rs')
| -rwxr-xr-x | 2020/Rust/src/day2.rs | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/2020/Rust/src/day2.rs b/2020/Rust/src/day2.rs new file mode 100755 index 0000000..4785b3f --- /dev/null +++ b/2020/Rust/src/day2.rs @@ -0,0 +1,64 @@ +struct Policy { + min: u32, + max: u32, + letter: char, + password: String, +} + +impl From<&str> for Policy { + fn from(value: &str) -> Self { + let (left, right) = value.split_once(':').unwrap(); + let (range, letter) = left.split_once(' ').unwrap(); + let (min, max) = range.split_once("-").unwrap(); + let password = right.trim_start().to_string(); + + Policy { + min: min.parse::<u32>().unwrap(), + max: max.parse::<u32>().unwrap(), + letter: letter.chars().next().unwrap(), + password, + } + } +} + +fn check_policy(policy: &Policy) -> bool { + let count = policy.password.matches(policy.letter).count() as u32; + count >= policy.min && count <= policy.max +} + +fn check_policy_part_two(policy: &Policy) -> bool { + let mut count = 0; + let policy_password = policy.password.as_bytes(); + if policy_password[(policy.min - 1) as usize] == policy.letter as u8 { + count += 1; + } + if policy_password[(policy.max - 1) as usize] == policy.letter as u8 { + count += 1; + } + count == 1 +} + +fn solve_part_one(input: &str) -> usize { + input + .lines() + .map(|line| Policy::from(line)) + .filter(|policy| check_policy(policy)) + .count() +} + +fn solve_part_two(input: &str) -> usize { + input + .lines() + .map(|line| Policy::from(line)) + .filter(|policy| check_policy_part_two(policy)) + .count() +} + +fn main() { + let test = include_str!("../input/day_2.test"); + let prod = include_str!("../input/day_2.prod"); + println!("Test_1: {}", solve_part_one(test)); + println!("Prod_1: {}", solve_part_one(prod)); + println!("Test_2: {}", solve_part_two(test)); + println!("Prod_2: {}", solve_part_two(prod)); +} |
