aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar alecdwm 2019-12-04 16:40:57 +1000
committerGravatar alecdwm 2019-12-04 16:40:57 +1000
commit2836cac30b04ca40ec72bc116548f0c9b65a30dd (patch)
tree70ad47b2adf9b1b99760c84c4193529a76f13263
parent2e961ec69e42b230d4dad3c53a3c8dc675ce0617 (diff)
added 2019 day4 part2 solution
-rw-r--r--src/main.rs1
-rw-r--r--src/year_2019/day4.rs74
2 files changed, 71 insertions, 4 deletions
diff --git a/src/main.rs b/src/main.rs
index 66df055..c0cf188 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -26,6 +26,7 @@ fn main() {
puzzle_solutions.insert("2019::day3::part1", advent_of_code::year_2019::day3::part1);
puzzle_solutions.insert("2019::day3::part2", advent_of_code::year_2019::day3::part2);
puzzle_solutions.insert("2019::day4::part1", advent_of_code::year_2019::day4::part1);
+ puzzle_solutions.insert("2019::day4::part2", advent_of_code::year_2019::day4::part2);
let command = match env::args().skip(1).next() {
Some(command) => command,
diff --git a/src/year_2019/day4.rs b/src/year_2019/day4.rs
index 30d7ef8..b96f1eb 100644
--- a/src/year_2019/day4.rs
+++ b/src/year_2019/day4.rs
@@ -24,7 +24,7 @@ pub fn part1() {
let number_of_passwords = range
.map(|integer| integer.to_string())
- .filter(|password| test_password_against_facts(password.as_str()))
+ .filter(|password| part1_test_password_against_facts(password.as_str()))
.count();
println!(
@@ -33,21 +33,78 @@ pub fn part1() {
);
}
-fn test_password_against_facts(password: &str) -> bool {
+/// An Elf just remembered one more important detail: the two adjacent matching digits are not part of a larger group of matching digits.
+///
+/// Given this additional criterion, but still ignoring the range rule, the following are now true:
+///
+/// 112233 meets these criteria because the digits never decrease and all repeated digits are exactly two digits long.
+/// 123444 no longer meets the criteria (the repeated 44 is part of a larger group of 444).
+/// 111122 meets the criteria (even though 1 is repeated more than twice, it still contains a double 22).
+///
+/// How many different passwords within the range given in your puzzle input meet all of the criteria?
+pub fn part2() {
+ let input = crate::common::read_stdin_to_string();
+ let range = ops::RangeInclusive::<i64>::from_str(input.as_str());
+
+ let number_of_passwords = range
+ .map(|integer| integer.to_string())
+ .filter(|password| part2_test_password_against_facts(password.as_str()))
+ .count();
+
+ println!(
+ "The number of different passwords within the range given which meet all of the criteria: {}",
+ number_of_passwords
+ );
+}
+
+fn part1_test_password_against_facts(password: &str) -> bool {
if password.len() != 6 {
return false;
}
let chars: Vec<char> = password.chars().collect();
+ if chars.windows(2).any(|window| window[0] > window[1]) {
+ return false;
+ }
+
if !chars.windows(2).any(|window| window[0] == window[1]) {
return false;
}
+ true
+}
+
+fn part2_test_password_against_facts(password: &str) -> bool {
+ if password.len() != 6 {
+ return false;
+ }
+
+ let chars: Vec<char> = password.chars().collect();
+
if chars.windows(2).any(|window| window[0] > window[1]) {
return false;
}
+ let mut groups: Vec<Vec<char>> = vec![vec![*chars
+ .first()
+ .expect("Failed to get first character of password")]];
+
+ chars.windows(2).for_each(|window| {
+ if window[0] == window[1] {
+ groups
+ .last_mut()
+ .expect("Failed to get_mut last group")
+ .push(window[1]);
+ } else {
+ groups.push(vec![window[1]]);
+ }
+ });
+
+ if !groups.iter().any(|group| group.len() == 2) {
+ return false;
+ }
+
true
}
@@ -75,11 +132,20 @@ mod tests {
use super::*;
#[test]
- fn test_password_factchecker() {
+ fn test_part1_password_factchecker() {
let examples = [("112233", true), ("223450", false), ("123789", false)];
for example in &examples {
- assert_eq!(test_password_against_facts(example.0), example.1);
+ assert_eq!(part1_test_password_against_facts(example.0), example.1);
+ }
+ }
+
+ #[test]
+ fn test_part2_password_factchecker() {
+ let examples = [("112233", true), ("123444", false), ("111122", true)];
+
+ for example in &examples {
+ assert_eq!(part2_test_password_against_facts(example.0), example.1);
}
}
}