aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar alecdwm 2019-12-04 17:38:02 +1000
committerGravatar alecdwm 2019-12-04 17:38:02 +1000
commit7657664485f668d2de1878fa790c08d1dec89e6b (patch)
treed86eb2a09bc494cccd617e4a2886ee4e56667b6b
parent2836cac30b04ca40ec72bc116548f0c9b65a30dd (diff)
refactored 2019 day4 part2 solution
-rw-r--r--Cargo.lock19
-rw-r--r--Cargo.toml1
-rw-r--r--src/year_2019/day4.rs51
3 files changed, 48 insertions, 23 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 4389501..946d2a6 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -3,4 +3,23 @@
[[package]]
name = "advent-of-code"
version = "0.1.0"
+dependencies = [
+ "itertools 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+[[package]]
+name = "either"
+version = "1.5.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "itertools"
+version = "0.8.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[metadata]
+"checksum either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "bb1f6b1ce1c140482ea30ddd3335fc0024ac7ee112895426e0a629a6c20adfe3"
+"checksum itertools 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f56a2d0bc861f9165be4eb3442afd3c236d8a98afd426f65d92324ae1091a484"
diff --git a/Cargo.toml b/Cargo.toml
index 3459379..16e7ca4 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -5,3 +5,4 @@ authors = ["alecdwm <alec@owls.io>"]
edition = "2018"
[dependencies]
+itertools = "0.8.2"
diff --git a/src/year_2019/day4.rs b/src/year_2019/day4.rs
index b96f1eb..158e82f 100644
--- a/src/year_2019/day4.rs
+++ b/src/year_2019/day4.rs
@@ -1,5 +1,6 @@
//! --- Day 4: Secure Container ---
+use itertools::Itertools;
use std::ops;
/// You arrive at the Venus fuel depot only to discover it's protected by a password. The Elves had written the password on a sticky note, but someone threw it out.
@@ -58,17 +59,26 @@ pub fn part2() {
}
fn part1_test_password_against_facts(password: &str) -> bool {
+ // length must be 6
if password.len() != 6 {
return false;
}
- let chars: Vec<char> = password.chars().collect();
-
- if chars.windows(2).any(|window| window[0] > window[1]) {
+ // digits must not decrease
+ if password
+ .chars()
+ .zip(password.chars().skip(1))
+ .any(|window| window.0 > window.1)
+ {
return false;
}
- if !chars.windows(2).any(|window| window[0] == window[1]) {
+ // at least two concurrent digits must be the same
+ if !password
+ .chars()
+ .zip(password.chars().skip(1))
+ .any(|window| window.0 == window.1)
+ {
return false;
}
@@ -76,32 +86,27 @@ fn part1_test_password_against_facts(password: &str) -> bool {
}
fn part2_test_password_against_facts(password: &str) -> bool {
+ // length must be 6
if password.len() != 6 {
return false;
}
- let chars: Vec<char> = password.chars().collect();
-
- if chars.windows(2).any(|window| window[0] > window[1]) {
+ // digits must not decrease
+ if password
+ .chars()
+ .zip(password.chars().skip(1))
+ .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) {
+ // at least one group of identical concurrent digits must be of length 2
+ if !password
+ .chars()
+ .group_by(|character| *character)
+ .into_iter()
+ .any(|(_character, group)| group.count() == 2)
+ {
return false;
}